MyBatis-Plus 分页查询

一 点睛

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

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

MyBatis-Plus 分页查询_第1张图片

二 实战

1 数据库中的数据

mysql> select * from user;
+---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
| id                  | NAME         | age  | email              | create_time         | update_time         | version |
+---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
|                   0 | auto         |   20 | [email protected]        | NULL                | NULL                |    NULL |
|                   1 | Jone         |   18 | [email protected] | NULL                | NULL                |    NULL |
|                   2 | Jack         |   20 | [email protected] | NULL                | NULL                |    NULL |
|                   3 | Tom          |   28 | [email protected] | NULL                | NULL                |    NULL |
|                   4 | Sandy        |   21 | [email protected] | NULL                | NULL                |    NULL |
|                   5 | Billie       |   24 | [email protected] | NULL                | NULL                |    NULL |
| 1443158688033337346 | lucymaryupup |   20 | [email protected]        | NULL                | 2021-09-30 08:51:12 |    NULL |
| 1443378040145903617 | ASSIGN_ID    |   20 | [email protected]        | 2021-09-30 08:51:56 | 2021-09-30 08:51:56 |    NULL |
| 1444192337759502338 | zhangsan     |   20 | [email protected]        | 2021-10-02 14:47:40 | 2021-10-02 14:55:10 |       2 |
+---------------------+--------------+------+--------------------+---------------------+---------------------+---------+
9 rows in set (0.00 sec)

 2 配置分页插件

@Configuration
@MapperScan("com.cakin.demomptest.mapper")
public class MpConfig {
    /**
     * 乐观锁插件
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    /**
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

3 测试 selectPage 分页

// 分页查询
@Test
public void testSelectPage() {
    Page page = new Page(1, 3);
    Page userPage = userMapper.selectPage(page, null);
    // 返回对象得到分页所有数据
    long pages = userPage.getPages(); // 总页数
    long current = userPage.getCurrent(); // 当前页
    List records = userPage.getRecords(); // 查询数据集合
    long total = userPage.getTotal(); // 总记录数
    boolean hasNext = userPage.hasNext();  // 下一页
    boolean hasPrevious = userPage.hasPrevious(); // 上一页

    System.out.println(pages);
    System.out.println(current);
    System.out.println(records);
    System.out.println(total);
    System.out.println(hasNext);
    System.out.println(hasPrevious);
}

4 查询结果

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

5 测试 selectMapsPage 分页

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

// 分页查询
@Test
public void testSelectMapsPage() {
    // Page 不需要泛型
    Page> page = new Page<>(1, 5);
    Page> pageParam = userMapper.selectMapsPage(page, null);
    List> records = pageParam.getRecords();
    records.forEach(System.out::println);
    System.out.println(pageParam.getCurrent());
    System.out.println(pageParam.getPages());
    System.out.println(pageParam.getSize());
    System.out.println(pageParam.getTotal());
    System.out.println(pageParam.hasNext());
    System.out.println(pageParam.hasPrevious());
}

测试 selectMapsPage 分页:结果集是 Map

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

你可能感兴趣的:(MyBatis,mybatis)