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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   import tk.mybatis.mapper.entity.Example;
   import com.github.pagehelper.PageHelper;
  
...
  
    @Override
    public List<Repayxxx> listRepaymentPlan(Integer start) {
  
     
        Example example = new Example(Repayxxx.class);
        // 排序
        example.orderBy("id");
        // 条件查询
        example.createCriteria()
                .andNotEqualTo("repayxxx", 3)
                .andLessThanOrEqualTo("xxxRepayDate", new Date());
        // 分页
        PageHelper.startPage(start, 20); // 每次查询20条
  
        return repaymentPlanMapper.selectByExample(example);
    }

  关于排序还有这些写法:

1
2
3
4
5
6
7
8
// 注意:排序使用的是表中的列名,不是对象属性名。
example.setOrderByClause("time DESC");
  
example.setOrderByClause ("product_code desc , product_name desc");
  
// 注意:排序使用的是对象属性。
example.orderBy('id').asc().orderBy('name').desc();

  

2. PageHelper 使用详解见文章:分页插件pageHelpler的使用(ssm框架中)服务器端分页 https://blog.csdn.net/jiangyu1013/article/details/56287388

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

java 查询功能实现的八种方式 https://blog.csdn.net/jiangyu1013/article/details/90032529

MyBatis : Mapper 接口以及 Example 使用实例、详解  https://blog.csdn.net/jiangyu1013/article/details/88689767

 

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

1
PageHelper.startPage(第几页, 20,false); // 每次查询20条

  

当数据量极大时,可以快速查询,忽略总条数的查询,减少查询时间。

以下是该方法原码实现:

 

 

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

 

 

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

1
2
3
4
5
6
7
8
9
10
public List<RepayPlan> listRepayPlan(int start) {
        
       Example example = new Example(RepayPlan.class);
       example.orderBy("id "); // 按id排序
       example.createCriteria()
               .andNotEqualTo("repayxxx", 3)
               .andLessThanOrEqualTo("xxxRepayDate", new Date());
       RowBounds rowBounds = new RowBounds(start, 20); // 每次查询20条
       return epaymentPlanMapper.selectByExampleAndRowBounds(example,rowBounds);
   }

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

posted @   浅笑19  阅读(4308)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示