mybatisplus+springboot+mybatisx联合步骤,并且应该如何在controller写
时间: 2023-06-29 17:04:54 浏览: 87
MybatisPlus 是 Mybatis 的增强工具,可以简化 Mybatis 的开发。MybatisX 是一款 Mybatis 开发插件,可以提高 Mybatis 的开发效率。下面是 MybatisPlus+SpringBoot+MybatisX 的联合步骤:
1. 在 pom.xml 文件中添加 MybatisPlus 和 MybatisX 的依赖。
```xml
<!-- MybatisPlus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<!-- MybatisX -->
<dependency>
<groupId>io.github.mybatisx</groupId>
<artifactId>mybatisx-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
```
2. 配置 MybatisPlus 和 MybatisX。
```java
@Configuration
public class MybatisConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
@Bean
public MybatisXConfigurer mybatisXConfigurer() {
return new MybatisXConfigurer();
}
}
```
3. 编写实体类和 Mapper 接口,使用 MybatisPlus 提供的注解。
```java
@Data
@TableName("user")
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String username;
private String password;
}
public interface UserMapper extends BaseMapper<User> {
}
```
4. 在 application.yml 中配置数据库连接信息和 MybatisPlus 的相关信息。
```yaml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: root
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
type-aliases-package: com.example.demo.entity
global-config:
db-config:
id-type: auto
field-strategy: not_null
logic-delete-value: 1
logic-not-delete-value: 0
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
mybatisx:
enabled: true
```
5. 在 Controller 中使用 Mapper 接口进行数据库操作。
```java
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/user/{id}")
public User getUser(@PathVariable("id") Long id) {
return userMapper.selectById(id);
}
@PostMapping("/user")
public boolean addUser(@RequestBody User user) {
return userMapper.insert(user) > 0;
}
@PutMapping("/user")
public boolean updateUser(@RequestBody User user) {
return userMapper.updateById(user) > 0;
}
@DeleteMapping("/user/{id}")
public boolean deleteUser(@PathVariable("id") Long id) {
return userMapper.deleteById(id) > 0;
}
}
```
这样,就完成了 MybatisPlus+SpringBoot+MybatisX 的整合。在 Controller 中,可以直接注入 Mapper 接口进行数据库操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)