问题描述:
分页失效,mapper.selectPage返回记录,total还是0,往上大多数问题都是老版本的解决方式,mybatis-plus 3.4.x 版本无法解决

原因:mybatis-plus 3.4.x 貌似通过拦截器进行分页的,这里没有启用,主要原因是数据库配置没有引入,以下为代码情况:
1:xml依赖情况
<!--整合mybatis plus https://baomidou.com/-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>2:增加配置类
@Configuration
@MapperScan(value = "com.hwxc.storeassistant.mapper.db1",sqlSessionFactoryRef = "sqlSessionFactoryBean1")
public class MyBatisConfigOne {
@Autowired
@Qualifier("db1")
DataSource db1;
@Bean
public SqlSessionFactory sqlSessionFactoryBean1(MybatisPlusInterceptor mybatisPlusInterceptor) throws Exception {
// 解决 Invalid bound statement 问题,必须使用 MybatisSqlSessionFactoryBean
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(db1);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
//注意这里,需要引入拦截器才能使分页生效
factoryBean.setPlugins(mybatisPlusInterceptor);
return factoryBean.getObject();
}
/**
* 来源官网:https://baomidou.com/pages/2976a3/#spring
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//引入分页拦截器
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setDbType(DbType.ORACLE);
paginationInnerInterceptor.setMaxLimit(500L);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
}以上就能解决mybatis-plus 3.4.x版无法分页的问题
版权声明:本文为m0_59061013原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。