原帖地址: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 快。