2.MyBatis-Plus 使用分页查询, Total 和 Pages 都是0
可能的原因:
原因1:声明Page对象时将 isSearchCount
参数设为了false
如下:
Page<ExampleUser> page = new Page<>(1, 3, false);
解决方法:将 isSearchCount
参数设为了true
表示查询总量
**注意:**此操作会导致每次查询两条sql语句
Page<ExampleUser> page = new Page<>(1, 3, true);
原因2:将默认的 SqlSessionFactory
调整为了 Mybatis-Plus 的MybatisSqlSessionFactoryBean
并且没有将 分页插件 MybatisPlusInterceptor
配置到 Mybatis-Plus 的MybatisSqlSessionFactoryBean
解决方法:将分页插件 MybatisPlusInterceptor
配置到 Mybatis-Plus 的MybatisSqlSessionFactoryBean
@Configuration
public class MyBatisPlusConfig {
@Autowired
@Qualifier(value = "dataSource")
DataSource dataSource;
@Autowired
MybatisPlusInterceptor paginationInnerInterceptor;
// 调整 SqlSessionFactory 为 Mybatis-Plus 的 MybatisSqlSessionFactoryBean
@Bean("sqlSessionFactory")
MybatisSqlSessionFactoryBean bean() {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
// 设置映射文件路径
bean.setMapperLocations(new ClassPathResource("mapper/sys/ExampleUserMapper.xml"));
// 设置分页插件
bean.setPlugins(paginationInnerInterceptor);
return bean;
}
/**
* myBatisPlus 分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return mybatisPlusInterceptor;
}
}