mybatis 中 Example 的使用 :条件查询、排序、分页(三种分页方式 : RowBounds、PageHelpler 、limit )
生活随笔
收集整理的這篇文章主要介紹了
mybatis 中 Example 的使用 :条件查询、排序、分页(三种分页方式 : RowBounds、PageHelpler 、limit )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。?
import tk.mybatis.mapper.entity.Example;import com.github.pagehelper.PageHelper;...@Overridepublic 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框架中)服務器端分頁
3. 更多關于 Example 的使用說明見文章:
java 查詢功能實現的八種方式
MyBatis : Mapper 接口以及 Example 使用實例、詳解?
?
4. 當只是查詢數據,不需要返回總條數時可選擇此方法:
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("repayxxx", 3).andLessThanOrEqualTo("xxxRepayDate", new Date());RowBounds rowBounds = new RowBounds(start, 20); // 每次查詢20條return epaymentPlanMapper.selectByExampleAndRowBounds(example,rowBounds);}推薦用 RowBounds :mybatis 自帶的,且速度快 。個人運行,后 2 種分頁明顯比 PageHelper 快。
總結
以上是生活随笔為你收集整理的mybatis 中 Example 的使用 :条件查询、排序、分页(三种分页方式 : RowBounds、PageHelpler 、limit )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#使用HttpClient调用WebA
- 下一篇: 电脑如何获得管理员权限