mybatis 中 Example 的使用 :條件查詢、排序、分頁(三種分頁方式 : RowBounds、PageHelpler 、limit )
阿新 • • 發佈:2022-01-08
原帖地址: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); }
-
PageHelper 使用詳解見文章:分頁外掛pageHelpler的使用(ssm框架中)伺服器端分頁
-
更多關於 Example 的使用說明見文章:
java 查詢功能實現的八種方式
MyBatis : Mapper 介面以及 Example 使用例項、詳解
- 當只是查詢資料,不需要返回總條數時可選擇此方法:
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 快。