MybatisPlus实现分页
1. 添加分页插件
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
2. 编写分页代码
1).创建page对象
2).调用mp分页查询的方法
3).通过page对象获取分页数据
/**
* 测试分页功能
*/
@Test
void testPage(){
//创建page对象
//传入两个参数。当前页和每页显示记录数
Page<User> userPage=new Page<>(1,3);
//调用mp分页查询的方法
//调用mp查询过程中,底层封装
//把分页的所有数据封装到page对象中,
userMapper.selectPage(userPage,null);
//通过page对象获取分页数据
System.out.println(userPage.getCurrent());//获取当前页
System.out.println(userPage.getRecords());//每页数据的list集合
System.out.println(userPage.getSize());//每页显示的记录数
System.out.println(userPage.getTotal());//总记录数
System.out.println(userPage.getPages());//每页数
System.out.println(userPage.hasNext());//下一页
System.out.println(userPage.hasPrevious());//上一页
}
}
3.控制台显示数据
表中总记录11条:
还没有评论,来说两句吧...