springmyboot+batis的代码 1.带有依赖,yml配置,mapper.xml,entity,mapper,service,controller,application 2.要有注释 3.entity为user ,属性为id,username,password
时间: 2024-05-11 08:13:47 浏览: 87
1.带有依赖,yml配置,mapper.xml,entity,mapper,service,controller,application
pom.xml
```xml
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MyBatis Starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- Spring Boot Starter Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
```
application.yml
```yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
main:
web-application-type: servlet
mybatis:
mapper-locations: classpath:mapper/*.xml
```
UserMapper.xml
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="UserMap" type="com.example.entity.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
</resultMap>
<select id="getUserById" resultMap="UserMap" parameterType="int">
SELECT * FROM user WHERE id = #{id}
</select>
<select id="getUserByUsername" resultMap="UserMap" parameterType="string">
SELECT * FROM user WHERE username = #{username}
</select>
<insert id="insertUser" parameterType="com.example.entity.User">
INSERT INTO user(username, password) VALUES (#{username}, #{password})
</insert>
<update id="updateUser" parameterType="com.example.entity.User">
UPDATE user SET username = #{username}, password = #{password} WHERE id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
</mapper>
```
User.java
```java
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
UserMapper.java
```java
@Mapper
public interface UserMapper {
User getUserById(int id);
User getUserByUsername(String username);
void insertUser(User user);
void updateUser(User user);
void deleteUser(int id);
}
```
UserService.java
```java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(int id) {
return userMapper.getUserById(id);
}
public User getUserByUsername(String username) {
return userMapper.getUserByUsername(username);
}
public void insertUser(User user) {
userMapper.insertUser(user);
}
public void updateUser(User user) {
userMapper.updateUser(user);
}
public void deleteUser(int id) {
userMapper.deleteUser(id);
}
}
```
UserController.java
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User getUserById(@PathVariable int id) {
return userService.getUserById(id);
}
@GetMapping("/username/{username}")
public User getUserByUsername(@PathVariable String username) {
return userService.getUserByUsername(username);
}
@PostMapping("/")
public void insertUser(@RequestBody User user) {
userService.insertUser(user);
}
@PostMapping("/{id}")
public void updateUser(@PathVariable int id, @RequestBody User user) {
user.setId(id);
userService.updateUser(user);
}
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable int id) {
userService.deleteUser(id);
}
}
```
Application.java
```java
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
2.要有注释
注释已添加在代码中。
3.entity为user,属性为id,username,password
User.java
```java
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
阅读全文