MybatisPlus实现分页

矫情吗;* 2021-09-25 06:28 790阅读 0赞

1. 添加分页插件

  1. /**
  2. * 分页插件
  3. */
  4. @Bean
  5. public PaginationInterceptor paginationInterceptor() {
  6. return new PaginationInterceptor();
  7. }

2. 编写分页代码

1).创建page对象

2).调用mp分页查询的方法

3).通过page对象获取分页数据

  1. /**
  2. * 测试分页功能
  3. */
  4. @Test
  5. void testPage(){
  6. //创建page对象
  7. //传入两个参数。当前页和每页显示记录数
  8. Page<User> userPage=new Page<>(1,3);
  9. //调用mp分页查询的方法
  10. //调用mp查询过程中,底层封装
  11. //把分页的所有数据封装到page对象中,
  12. userMapper.selectPage(userPage,null);
  13. //通过page对象获取分页数据
  14. System.out.println(userPage.getCurrent());//获取当前页
  15. System.out.println(userPage.getRecords());//每页数据的list集合
  16. System.out.println(userPage.getSize());//每页显示的记录数
  17. System.out.println(userPage.getTotal());//总记录数
  18. System.out.println(userPage.getPages());//每页数
  19. System.out.println(userPage.hasNext());//下一页
  20. System.out.println(userPage.hasPrevious());//上一页
  21. }
  22. }

3.控制台显示数据

表中总记录11条:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxOTUwNDQ3_size_16_color_FFFFFF_t_70

20210401181004874.png

发表评论

表情:
评论列表 (有 0 条评论,790人围观)

还没有评论,来说两句吧...

相关阅读