mybatis-plus里面的Page

本文讲解mybatis-plus里面的Page类。

详细讲解

MyBatis-Plus 中的 Page 类

MyBatis-Plus 是基于 Mybatis 的增强工具,在提供通用 CRUD 方法之外,还提供了分页查询等功能,其中最重要的就是 Page<T> 类。Page<T> 用于封装分页查询的结果,通过对该类进行配置可以实现灵活的分页查询。

Page 类的构造方法

public Page() {
    this(0L, 10L);
}
public Page(long current, long size) {
    this(current, size, 0L);
}
public Page(long current, long size, long total) {
    this(current, size, total, true);
}
public Page(long current, long size, boolean count) {
    this(current, size, 0L, count);
}
public Page(long current, long size, long total, boolean searchCount) {
    this.records = Collections.emptyList();
    this.current = 1L;
    this.size = 10L;
    this.total = 0L;
    this.pages = 0L;
    this.optimizeCountSql = true;
    this.isSearchCount = true;
    this.hitCount = false;
    this.countId = "mp_count";
    this.maxLimit = -1L;
    if (current > 1L) {
        this.current = current;
    }
    this.size = size;
    this.total = total;
    this.isSearchCount = searchCount;
}

从上面的代码可以看出,Page<T> 支持五种构造方式,它们分别是:

  • new Page():使用默认分页参数,即当前页为 1、每页记录数为 10。
  • new Page(long current, long size):传入当前页码和每页记录数来构造一个分页对象,未传入总记录数即表示不查询总记录数。
  • new Page(long current, long size, boolean count):传入当前页码、每页记录数和是否查询总记录数,其中 count=true 表示查询总记录数,count=false 表示不查询总记录数。
  • new Page(long current, long size, long total):传入当前页码、每页记录数和总记录数来构造一个分页对象。
  • new Page(long current, long size, long total, boolean searchCount):传入当前页码、每页记录数、总记录数和是否查询总记录数来构造一个分页对象。

除此之外,Page<T> 还有许多其它有用的方法,比如获取当前页码、每页记录数、总记录数等数据,以及计算出分页情况(如总页数、起始位置等)等。

Mybatis-Plus 配置文件中的使用

在进行分页查询时,可以在 MyBatis 的 XML 映射文件中指定 pagesize 两个属性来实现。而如果你想自己手动构建 Page<T> 对象,还可以在配置文件中通过 paginatorType 属性来指定使用什么类型的分页工具类,如下:

<!-- 使用 MyBatis-Plus 提供的 Paginator 类作为分页工具 -->
<plugins>
    <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
        <property name="dialectType" value="mysql"/>
        <property name="optimizerType" value="count"/>
        <property name="pagination" value="true"/>
        <!-- 指定使用 Mybatis-Plus 提供的 Paginator 类作为分页工具 -->
        <property name="paginatorType" value="com.baomidou.mybatisplus.extension.plugins.pagination.Paginator"/>
    </plugin>
</plugins>

如果你不需要查询总记录数,可以通过 searchCount 属性禁用掉。这样做会稍微提升一点性能。

<!-- 禁用查询总记录数 -->
<plugins>
    <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
        <property name="dialectType" value="mysql"/>
        <property name="optimizerType" value="count"/>
        <property name="pagination" value="true"/>
        <property name="searchCount" value="false"/>
    </plugin>
</plugins>

分页查询示例

Page<Order> page = new Page<>(1, 10);
QueryWrapper<Order> wrapper = new QueryWrapper<>();
wrapper.eq("user_id", 1L);
IPage<Order> orderPage = orderMapper.selectPage(page, wrapper);
List<Order> orders = orderPage.getRecords();
long total = orderPage.getTotal();

上面的代码使用 Page<T> 对象进行分页查询。其中,Order 是我们自定义的订单类,orderMapper 是 MyBatis 的 Mapper 文件,wrapper 则表示条件查询对象。通过 selectPage(page, wrapper) 方法即可完成分页查询,返回一个 IPage<T> 对象。

在这里,我们还可以通过 getRecords() 方法获取查询结果,通过 getTotal() 方法获取总记录数。如果不需要查询总记录数,也可以通过 searchCount 属性在配置文件中关闭掉。