1.配置分页插件
@Configuration
public class MyBatisConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//1.创建分页插件
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
paginationInnerInterceptor.setMaxLimit(1000L);//设置分页查询的最大限制
//2.添加分页插件
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
}
2.测试
@Test
void PageQuery() {
int pageNo = 1, pageSize = 2;
//1.准备分页条件
Page<User> page = Page.of(pageNo, pageSize);
//排序条件,第一个条件相同,比第二个条件
page.addOrder(new OrderItem("age", true));
//...第二个条件
//2.分页查询
Page<User> p = userService.page(page);
//3.解析
long total = p.getTotal();
System.out.println("total = " + total);
long pages = p.getPages();
System.out.println("pages = "+pages);
List<User> users = p.getRecords();
users.forEach(System.out::println);
}
通用分页实体(有更好的,封装业务逻辑)