mybatis-plus里面的Page

  • 作者简介:一名后端开发人员,每天分享后端开发以及人工智能相关技术,行业前沿信息,面试宝典。
  • 座右铭:未来是不可确定的,慢慢来是最快的。
  • 个人主页极客李华-CSDN博客
  • 合作方式:私聊+
  • 这个专栏内容:BAT等大厂常见后端java开发面试题详细讲解,更新数目100道常见大厂java后端开发面试题。
  • 我的CSDN社区:https://bbs.csdn.net/forums/99eb3042821a4432868bb5bfc4d513a8
  • 微信公众号,抖音,b站等平台统一叫做:极客李华,加入微信公众号领取各种编程资料,加入抖音,b站学习面试技巧,职业规划

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 属性在配置文件中关闭掉。

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis-Plus is an enhanced version of MyBatis, a popular open-source Java persistence framework. It provides additional features and utilities to simplify database operations. To perform a pagination query using MyBatis-Plus, you can use the `selectPage` method provided by the `com.baomidou.mybatisplus.core.mapper.BaseMapper` interface. Here's an example of how to use the `selectPage` method: ```java import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.core.metadata.IPage; // Assuming you have a mapper interface that extends BaseMapper public interface YourMapper extends BaseMapper<YourEntity> { } // In your service or repository class @Autowired private YourMapper yourMapper; public IPage<YourEntity> getEntityPage(int pageNo, int pageSize) { // Create a Page object with the desired page number and page size Page<YourEntity> page = new Page<>(pageNo, pageSize); // Create a QueryWrapper object for your query conditions (if any) QueryWrapper<YourEntity> queryWrapper = new QueryWrapper<>(); // Perform the pagination query using selectPage return yourMapper.selectPage(page, queryWrapper); } ``` In the above example, `YourEntity` represents your entity class, `YourMapper` is the mapper interface extending `BaseMapper`, and `yourMapper` is an instance of the mapper interface injected using the `@Autowired` annotation. The `getEntityPage` method takes two arguments: `pageNo` for the desired page number and `pageSize` for the number of records per page. It creates a `Page` object with these parameters and a `QueryWrapper` object for any query conditions. Finally, it calls the `selectPage` method on the mapper interface to perform the pagination query and returns the resulting `IPage` object. You can customize the `QueryWrapper` object to add various query conditions such as equals, greater than, less than, etc., depending on your requirements. Please note that this is just a basic example, and there are many other features and options available in MyBatis-Plus for pagination and database operations. You can refer to the official documentation for more details: [MyBatis-Plus Documentation](https://mybatis.plus/)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客李华

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值