背景:
项目需要从mybits切换到mubits-plus,但是我在进行分页查询的时候,发现一直不生效
问题原因:
添加监听器,配置如下:
@Configuration
@MapperScan("com.baomidou.mybatisplus.samples.deluxe.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
/**
* 自定义 SqlInjector
* 里面包含自定义的全局方法
*/
@Bean
public MyLogicSqlInjector myLogicSqlInjector() {
return new MyLogicSqlInjector();
}
}
问题定位过程:
如果不感兴趣着,可以在上面复制答案,直接操作就行
debug看看啥原因:
入参:
debug看没有问题
结果:
两条数据同时没有总数,不符合要求
看日志:
没有拼接分页查询的动作
那么什么原因的
1.查看官网文档
网管地址:
官网有些水,没看到具体信息
2.查看案例
github地址
GitHub - baomidou/mybatis-plus-samples: MyBatis-Plus Samples
在案例中找到了错误原因:
需要配置监听器
按照下面的方式添加就行
@Configuration
@MapperScan("com.baomidou.mybatisplus.samples.deluxe.mapper")
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
/**
* 自定义 SqlInjector
* 里面包含自定义的全局方法
*/
@Bean
public MyLogicSqlInjector myLogicSqlInjector() {
return new MyLogicSqlInjector();
}
}