首页 > 技术文章 > MyBatis-Plus 分页插件过时

gtnotgod 2020-11-09 16:22 原文

引用: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);

}
 

推荐阅读