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); }
關於排序還有這些寫法:
// 注意:排序使用的是表中的列名,不是對象屬性名。 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. 當只是查詢數據,不需要返回總條數時可選擇此方法:
PageHelper.startPage(第幾頁, 20,false); // 每次查詢20條
當數據量極大時,可以快速查詢,忽略總條數的查詢,減少查詢時間。
以下是該方法原碼實現:
1)分頁的寫法 下圖中黃框中的寫法運行 比紅框中 快,不知道是不是插件本身也會有費時:
2)再補充一種分頁方式,mybatis 自帶的 RowBounds:
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 快。