引用:https://blog.csdn.net/zyw562123314/article/details/108903456
//分頁插件老版本過時
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);
}