Mybatis-plus之RowBounds实现分页查询
生活随笔
收集整理的這篇文章主要介紹了
Mybatis-plus之RowBounds实现分页查询
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
物理分頁和邏輯分頁
物理分頁:直接從數(shù)據(jù)庫中拿出我們需要的數(shù)據(jù),例如在Mysql中使用limit。
邏輯分頁:從數(shù)據(jù)庫中拿出所有符合要求的數(shù)據(jù),然后再從這些數(shù)據(jù)中拿到我們需要的分頁數(shù)據(jù)。
優(yōu)缺點
物理分頁每次都要訪問數(shù)據(jù)庫,邏輯分頁只訪問一次。
物理分頁占用內(nèi)存少,邏輯分頁相對較多。
物理分頁數(shù)據(jù)每次都是最新的,邏輯分頁有可能滯后。
一般用法
1 public List<Order> queryListByPage(RowBounds rowBounds); 1 dao.queryListPage(new RowBounds(offset,limit));RowBounds對象有2個屬性,offset和limit。
offset:起始行數(shù)
limit:需要的數(shù)據(jù)行數(shù)
因此,取出來的數(shù)據(jù)就是:從第offset+1行開始,取limit行
Mybatis中使用RowBounds實現(xiàn)分頁的大體思路:
先取出所有數(shù)據(jù),然后游標(biāo)移動到offset位置,循環(huán)取limit條數(shù)據(jù),然后把剩下的數(shù)據(jù)舍棄。
1 private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException { 2 DefaultResultContext<Object> resultContext = new DefaultResultContext(); 3 this.skipRows(rsw.getResultSet(), rowBounds); //游標(biāo)跳到offset位置 4 //取出limit條數(shù)據(jù) 5 while(this.shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) { 6 ResultMap discriminatedResultMap = this.resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, (String)null); 7 Object rowValue = this.getRowValue(rsw, discriminatedResultMap); 8 this.storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet()); 9 } 10 11 } 1 private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException { 2 if (rs.getType() != 1003) { 3 if (rowBounds.getOffset() != 0) { 4 rs.absolute(rowBounds.getOffset()); 5 } 6 } else { //從頭開始移動游標(biāo),直至offset位置 7 for(int i = 0; i < rowBounds.getOffset(); ++i) { 8 rs.next(); 9 } 10 } 11 12 }在Mybatis-Plus中的應(yīng)用
Controller層
1 @RequestMapping(value = "list", method = { RequestMethod.GET, RequestMethod.POST }) 2 @PageableDefaults(sort = "createDate=desc") 3 private void getList(Queryable queryable,String queryStr, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request, 4 HttpServletResponse response) throws IOException { 5 //前端傳過來需要的參數(shù),加上id,fastjson會在得到結(jié)果集時過濾數(shù)據(jù) 6 propertyPreFilterable.addQueryProperty("id"); 7 QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass); 8 SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass); 9 //調(diào)用service層的分頁查詢 10 PageJson<OprPrintOrder> pagejson = new PageJson<OprPrintOrder>(service.list(queryable)); 11 //得到需要的結(jié)果集后的數(shù)據(jù)過濾操作 12 String content = JSON.toJSONString(pagejson, filter); 13 JSONObject result = JSONObject.parseObject(content); 14 StringUtils.printJson(response, result.toString()); 15 }Service層
1 @Override 2 public Page<Order> list(Queryable queryable) { 3 //pageable中有數(shù)據(jù)查詢的要求 4 Pageable pageable = queryable.getPageable(); 5 //封裝新的分頁查詢類 6 com.baomidou.mybatisplus.plugins.Page<Order> page = new com.baomidou.mybatisplus.plugins.Page<Order>(pageable.getPageNumber(), pageable.getPageSize()); 7 //傳入RowBounds,page就是RowBounds的子類,這樣查詢后page就有了總頁數(shù)與總條數(shù) 8 page.setRecords(mapper.selectList(page)); 9 return new PageImpl<Order>(page.getRecords(), pageable, page.getTotal()); 10 }Mapper層
1 List<Order> selectList(RowBounds rowBounds); 1 <select id="selectList" resultType="Order"> 2 select * from order 3 </select>轉(zhuǎn)載于:https://www.cnblogs.com/guanghe/p/10026099.html
總結(jié)
以上是生活随笔為你收集整理的Mybatis-plus之RowBounds实现分页查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡养卡这样养省钱又快速!想低成本养卡
- 下一篇: 兴业信用卡随借金怎么还?到期没有还后果很