mybatis 中 Example 的使用 :条件查询、排序、分页(三种分页方式 : RowBounds、PageHelpler 、limit )

原帖地址:https://cloud.tencent.com/developer/article/1433161

import tk.mybatis.mapper.entity.Example;
   import com.github.pagehelper.PageHelper;

...

    @Override
    public List<RepaymentPlan> listRepaymentPlan(Integer start) {

    
        Example example = new Example(RepaymentPlan.class);
        // 排序
        example.orderBy("id");
        // 条件查询
        example.createCriteria()
                .andNotEqualTo("repayStatus", 3)
                .andLessThanOrEqualTo("shouldRepayDate", new Date());
        // 分页
        PageHelper.startPage(start, 20); // 每次查询20条

        return repaymentPlanMapper.selectByExample(example);
    }
  1. PageHelper 使用详解见文章:分页插件pageHelpler的使用(ssm框架中)服务器端分页

  2. 更多关于 Example 的使用说明见文章:

java 查询功能实现的八种方式

MyBatis : Mapper 接口以及 Example 使用实例、详解

  1. 当只是查询数据,不需要返回总条数时可选择此方法:

PageHelper.startPage(第几页, 20,false); // 每次查询20条
当数据量极大时,可以快速查询,忽略总条数的查询,减少查询时间。

以下是该方法原码实现:


2019.5.13 后记 :

1)分页的写法 下图中黄框中的写法运行 比红框中 快,不知道是不是插件本身也会有费时:

2)再补充一种分页方式,mybatis 自带的 RowBounds:

 public List<RepayPlan> listRepayPlan(int start) {
        // 查询所有未还款结清且应还日期小于当前时间的账单
        Example example = new Example(RepayPlan.class);
        example.orderBy("id "); // 按id排序
        example.createCriteria()
                .andNotEqualTo("repayStatus", 3)
                .andLessThanOrEqualTo("shouldRepayDate", new Date());
        RowBounds rowBounds = new RowBounds(start, 20); // 每次查询20条
        return epaymentPlanMapper.selectByExampleAndRowBounds(example,rowBounds);
    }

推荐用 RowBounds :mybatis 自带的,且速度快 。个人运行,后 2 种分页明显比 PageHelper 快。

posted on   健力宝1995  阅读(1348)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示