MyBatis-Plus 分页查询

落日映苍穹つ 2022-08-28 10:54 411阅读 0赞

一 点睛

MyBatis Plus 自带分页插件,只要简单的配置即可实现分页功能。

分页查询的开发流程见下图。

watermark_type_ZHJvaWRzYW5zZmFsbGJhY2s_shadow_50_text_Q1NETiBAY2hlbmdxaXVtaW5n_size_20_color_FFFFFF_t_70_g_se_x_16

二 实战

1 数据库中的数据

  1. mysql> select * from user;
  2. +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
  3. | id | NAME | age | email | create_time | update_time | version |
  4. +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
  5. | 0 | auto | 20 | 1243@qq.com | NULL | NULL | NULL |
  6. | 1 | Jone | 18 | test1@baomidou.com | NULL | NULL | NULL |
  7. | 2 | Jack | 20 | test2@baomidou.com | NULL | NULL | NULL |
  8. | 3 | Tom | 28 | test3@baomidou.com | NULL | NULL | NULL |
  9. | 4 | Sandy | 21 | test4@baomidou.com | NULL | NULL | NULL |
  10. | 5 | Billie | 24 | test5@baomidou.com | NULL | NULL | NULL |
  11. | 1443158688033337346 | lucymaryupup | 20 | 1243@qq.com | NULL | 2021-09-30 08:51:12 | NULL |
  12. | 1443378040145903617 | ASSIGN_ID | 20 | 1243@qq.com | 2021-09-30 08:51:56 | 2021-09-30 08:51:56 | NULL |
  13. | 1444192337759502338 | zhangsan | 20 | 1243@qq.com | 2021-10-02 14:47:40 | 2021-10-02 14:55:10 | 2 |
  14. +---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
  15. 9 rows in set (0.00 sec)

2 配置分页插件

  1. @Configuration
  2. @MapperScan("com.cakin.demomptest.mapper")
  3. public class MpConfig {
  4. /**
  5. * 乐观锁插件
  6. */
  7. @Bean
  8. public OptimisticLockerInterceptor optimisticLockerInterceptor() {
  9. return new OptimisticLockerInterceptor();
  10. }
  11. /**
  12. * 分页插件
  13. */
  14. @Bean
  15. public PaginationInterceptor paginationInterceptor() {
  16. return new PaginationInterceptor();
  17. }
  18. }

3 测试 selectPage 分页

  1. // 分页查询
  2. @Test
  3. public void testSelectPage() {
  4. Page<User> page = new Page(1, 3);
  5. Page<User> userPage = userMapper.selectPage(page, null);
  6. // 返回对象得到分页所有数据
  7. long pages = userPage.getPages(); // 总页数
  8. long current = userPage.getCurrent(); // 当前页
  9. List<User> records = userPage.getRecords(); // 查询数据集合
  10. long total = userPage.getTotal(); // 总记录数
  11. boolean hasNext = userPage.hasNext(); // 下一页
  12. boolean hasPrevious = userPage.hasPrevious(); // 上一页
  13. System.out.println(pages);
  14. System.out.println(current);
  15. System.out.println(records);
  16. System.out.println(total);
  17. System.out.println(hasNext);
  18. System.out.println(hasPrevious);
  19. }

4 查询结果

  1. Creating a new SqlSession
  2. SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7f829c76] was not registered for synchronization because synchronization is not active
  3. 2021-10-02 15:55:10.203 INFO 1756 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
  4. 2021-10-02 15:55:12.209 INFO 1756 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
  5. JDBC Connection [HikariProxyConnection@668929853 wrapping com.mysql.cj.jdbc.ConnectionImpl@c35af2a] will not be managed by Spring
  6. JsqlParserCountOptimize sql=SELECT id,name,age,email,create_time,update_time,version FROM user
  7. ==> Preparing: SELECT COUNT(1) FROM user
  8. ==> Parameters:
  9. <== Columns: COUNT(1)
  10. <== Row: 9
  11. ==> Preparing: SELECT id,name,age,email,create_time,update_time,version FROM user LIMIT ?,?
  12. ==> Parameters: 0(Long), 3(Long)
  13. <== Columns: id, name, age, email, create_time, update_time, version
  14. <== Row: 0, auto, 20, 1243@qq.com, null, null, null
  15. <== Row: 1, Jone, 18, test1@baomidou.com, null, null, null
  16. <== Row: 2, Jack, 20, test2@baomidou.com, null, null, null
  17. <== Total: 3
  18. Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7f829c76]
  19. 3
  20. 1
  21. [User(id=0, name=auto, age=20, email=1243@qq.com, createTime=null, updateTime=null, version=null), User(id=1, name=Jone, age=18, email=test1@baomidou.com, createTime=null, updateTime=null, version=null), User(id=2, name=Jack, age=20, email=test2@baomidou.com, createTime=null, updateTime=null, version=null)]
  22. 9
  23. true
  24. false

5 测试 selectMapsPage 分页

当指定了特定的查询列时,希望分页结果列表只返回被查询的列,而不是很多 null 值

  1. // 分页查询
  2. @Test
  3. public void testSelectMapsPage() {
  4. // Page 不需要泛型
  5. Page<Map<String, Object>> page = new Page<>(1, 5);
  6. Page<Map<String, Object>> pageParam = userMapper.selectMapsPage(page, null);
  7. List<Map<String, Object>> records = pageParam.getRecords();
  8. records.forEach(System.out::println);
  9. System.out.println(pageParam.getCurrent());
  10. System.out.println(pageParam.getPages());
  11. System.out.println(pageParam.getSize());
  12. System.out.println(pageParam.getTotal());
  13. System.out.println(pageParam.hasNext());
  14. System.out.println(pageParam.hasPrevious());
  15. }

测试 selectMapsPage 分页:结果集是 Map

  1. Creating a new SqlSession
  2. SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ae7deac] was not registered for synchronization because synchronization is not active
  3. 2021-10-02 15:59:14.475 INFO 20468 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
  4. 2021-10-02 15:59:16.276 INFO 20468 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
  5. JDBC Connection [HikariProxyConnection@1018742990 wrapping com.mysql.cj.jdbc.ConnectionImpl@1fde0371] will not be managed by Spring
  6. JsqlParserCountOptimize sql=SELECT id,name,age,email,create_time,update_time,version FROM user
  7. ==> Preparing: SELECT COUNT(1) FROM user
  8. ==> Parameters:
  9. <== Columns: COUNT(1)
  10. <== Row: 9
  11. ==> Preparing: SELECT id,name,age,email,create_time,update_time,version FROM user LIMIT ?,?
  12. ==> Parameters: 0(Long), 5(Long)
  13. <== Columns: id, name, age, email, create_time, update_time, version
  14. <== Row: 0, auto, 20, 1243@qq.com, null, null, null
  15. <== Row: 1, Jone, 18, test1@baomidou.com, null, null, null
  16. <== Row: 2, Jack, 20, test2@baomidou.com, null, null, null
  17. <== Row: 3, Tom, 28, test3@baomidou.com, null, null, null
  18. <== Row: 4, Sandy, 21, test4@baomidou.com, null, null, null
  19. <== Total: 5
  20. Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6ae7deac]
  21. {name=auto, id=0, age=20, email=1243@qq.com}
  22. {name=Jone, id=1, age=18, email=test1@baomidou.com}
  23. {name=Jack, id=2, age=20, email=test2@baomidou.com}
  24. {name=Tom, id=3, age=28, email=test3@baomidou.com}
  25. {name=Sandy, id=4, age=21, email=test4@baomidou.com}
  26. 1
  27. 2
  28. 5
  29. 9
  30. true
  31. false

发表评论

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

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

相关阅读