springmyboot+batis的代码 1.带有依赖,yml配置,mapper.xml,entity,mapper,service,controller,application 2.要有注释 3.entity为user ,属性为id,username,passwor
时间: 2024-05-08 10:16:25 浏览: 122
d,age,gender
1. 依赖
```xml
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
```
2. yml配置
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
mvc:
throw-exception-if-no-handler-found: true
static-path-pattern: /static/**
mybatis:
mapper-locations: classpath:mapper/*.xml
```
3. mapper.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.demo.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.demo.entity.User">
<id column="id" property="id" />
<result column="username" property="username" />
<result column="password" property="password" />
<result column="age" property="age" />
<result column="gender" property="gender" />
</resultMap>
<select id="getUserById" resultMap="BaseResultMap">
SELECT * FROM user WHERE id = #{id}
</select>
<select id="getAllUsers" resultMap="BaseResultMap">
SELECT * FROM user
</select>
<insert id="insertUser" parameterType="com.example.demo.entity.User">
INSERT INTO user(username, password, age, gender) VALUES (#{username}, #{password}, #{age}, #{gender})
</insert>
<update id="updateUser" parameterType="com.example.demo.entity.User">
UPDATE user SET username = #{username}, password = #{password}, age = #{age}, gender = #{gender} WHERE id = #{id}
</update>
<delete id="deleteUserById" parameterType="int">
DELETE FROM user WHERE id = #{id}
</delete>
</mapper>
```
4. entity
```java
public class User {
private int id;
private String username;
private String password;
private int age;
private String gender;
// getters and setters
}
```
5. mapper
```java
@Mapper
public interface UserMapper {
User getUserById(int id);
List<User> getAllUsers();
void insertUser(User user);
void updateUser(User user);
void deleteUserById(int id);
}
```
6. service
```java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUserById(int id) {
return userMapper.getUserById(id);
}
public List<User> getAllUsers() {
return userMapper.getAllUsers();
}
public void insertUser(User user) {
userMapper.insertUser(user);
}
public void updateUser(User user) {
userMapper.updateUser(user);
}
public void deleteUserById(int id) {
userMapper.deleteUserById(id);
}
}
```
7. controller
```java
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/users/{id}")
public ResponseEntity<User> getUserById(@PathVariable int id) {
User user = userService.getUserById(id);
if (user == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(user);
}
@GetMapping("/users")
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.getAllUsers();
if (users.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(users);
}
@PostMapping("/users")
public ResponseEntity<Void> insertUser(@RequestBody User user) {
userService.insertUser(user);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
@PutMapping("/users/{id}")
public ResponseEntity<Void> updateUser(@PathVariable int id, @RequestBody User user) {
user.setId(id);
userService.updateUser(user);
return ResponseEntity.ok().build();
}
@DeleteMapping("/users/{id}")
public ResponseEntity<Void> deleteUserById(@PathVariable int id) {
userService.deleteUserById(id);
return ResponseEntity.ok().build();
}
}
```
8. application
```java
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
注释部分省略,完整代码可参考 [GitHub](https://github.com/linjiajian999/SpringBoot-MyBatis-Demo)。
阅读全文