MybatiPlus 分页插件PaginationInnerInterceptor - 冢本八云 - 博客园 (cnblogs.com)
@Configuration
public class CustomMyBatisPlusConfig {
/**
* 分页插件,一缓和二缓遵循mybatis的规则
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
// 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false
// paginationInnerInterceptor.setOverflow(false);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
// paginationInnerInterceptor.setMaxLimit(500L);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
}
@Test
public void testSelectPage(){
//构建分页条件第二页每页显示3条
Page<User> page=new Page<>(2,3);
//使用分页条件查询,不使用其他条件
userMapper.selectPage(page, null);
//获取分页后查询出的记录
List<User> records = page.getRecords();
records.forEach(System.out::println);
System.out.println("是否有下一页:"+page.hasNext());
System.out.println("是否有上一页:"+page.hasPrevious());
System.out.println("总记录数:"+page.getTotal());
}