1、导入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
2、配置
# 导入驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
# 配置数据源(nacos)
spring:
datasource:
username: root
password: root
url: jdbc:mysql:///:3306/yueji_pms?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&useSSL=false
driver-class-name: com.mysql.jdbc.Driver
3、使用mybatis-plus功能
# 等同于在xml映射接口上标记@Mapper
@MapperScan("com.yueji.product.dao")
# 告诉mybatis-plus映射文件的位置
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
# 逻辑删除配置
global-config:
db-config:
id-type: auto #主键策略自增
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
# 开启SQL日志
logging:
level:
com.yueji.product: debug
4、分页插件
@Configuration
@EnableTransactionManagement // 开启事务
@MapperScan("com.yueji.product.dao")
public class MybatisConfig {
// 分页插件
// 旧版
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
paginationInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInterceptor.setLimit(1000);
// 开启 count 的 join 优化,只针对部分 left join
// paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}