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