MyBatis-Plus 分頁插件過時


引用:https://blog.csdn.net/zyw562123314/article/details/108903456
//分頁插件老版本過時
舊版本配置 @Bean public PaginationInterceptor paginationInterceptor(){ return new PaginationInterceptor(); }

3.4.0版本對此部分有更新,如果是舊版本升級,會出現分頁失效問題,同時idea會提示PaginationInterceptor過時,新版本改用了MybatisPlusInterceptor
//分頁插件
/**
* 新的分頁插件,一緩和二緩遵循mybatis的規則,需要設置 MybatisConfiguration#useDeprecatedExecutor = false 避免緩存出現問題
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}

@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
模糊分頁查詢:
@Override
@Transactional
public LayResult selectListByLikeName(Integer page, Integer limit, String keywords) {
//分頁查詢器
Page<User> page1 = new Page<>(page, limit);
//條件查詢器
QueryWrapper<User> wrapper = new QueryWrapper<>();
//構造篩選條件
/* wrapper.like(keywords!=null,"name",keywords);
第一個參數:該參數是一個布爾類型,只有該參數是true時,才將like條件拼接到sql中;本例中,如果name字段不為空,則拼接name字段的like查詢條件;
第二個參數:該參數是數據庫中的字段名;
第三個參數:該參數值字段值;
* *///判斷模糊查詢是否為空,如果不為空,則拼接模糊查詢條件
if(keywords != null && !"".equals(keywords)){
wrapper.like("name",keywords);
}
//執行分頁篩選查詢

Page<User> userPage = userMapper.selectPage(page1, wrapper);
//找出需要的數據,封裝
Integer total = Math.toIntExact(userPage.getTotal());
Integer current = Math.toIntExact(userPage.getCurrent());
List<User> records = userPage.getRecords();

records.forEach(System.out::println);

return LayResult.OK("成功", current, records, total);

}
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM