springboot整合mybatisplus 2.3.3 示例
时间: 2023-11-21 11:54:59 浏览: 47
是整合SpringBoot + MybatisPlus的示例:
1. 首先,在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
2. 在application.properties文件中添加以下配置:
```properties
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# MybatisPlus配置
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
mybatis-plus.global-config.db-config.id-type=auto
```
3. 创建实体类:
```java
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
```
4. 创建Mapper接口:
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
```
5. 创建Service接口及实现类:
```java
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
```
6. 创建Controller类:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return userService.getById(id);
}
@PostMapping
public boolean saveUser(@RequestBody User user) {
return userService.save(user);
}
@PutMapping
public boolean updateUser(@RequestBody User user) {
return userService.updateById(user);
}
@DeleteMapping("/{id}")
public boolean deleteUser(@PathVariable Long id) {
return userService.removeById(id);
}
}
```