日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis-plus之RowBounds实现分页查询

發布時間:2023/12/13 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis-plus之RowBounds实现分页查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

物理分頁和邏輯分頁

物理分頁:直接從數據庫中拿出我們需要的數據,例如在Mysql中使用limit。

邏輯分頁:從數據庫中拿出所有符合要求的數據,然后再從這些數據中拿到我們需要的分頁數據。

優缺點

物理分頁每次都要訪問數據庫,邏輯分頁只訪問一次。

物理分頁占用內存少,邏輯分頁相對較多。

物理分頁數據每次都是最新的,邏輯分頁有可能滯后。

一般用法

1 public List<Order> queryListByPage(RowBounds rowBounds); 1 dao.queryListPage(new RowBounds(offset,limit));

RowBounds對象有2個屬性,offset和limit。

offset:起始行數

limit:需要的數據行數

因此,取出來的數據就是:從第offset+1行開始,取limit行

Mybatis中使用RowBounds實現分頁的大體思路:

先取出所有數據,然后游標移動到offset位置,循環取limit條數據,然后把剩下的數據舍棄。

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); //游標跳到offset位置 4 //取出limit條數據 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 { //從頭開始移動游標,直至offset位置 7 for(int i = 0; i < rowBounds.getOffset(); ++i) { 8 rs.next(); 9 } 10 } 11 12 }

在Mybatis-Plus中的應用

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 //前端傳過來需要的參數,加上id,fastjson會在得到結果集時過濾數據 6 propertyPreFilterable.addQueryProperty("id"); 7 QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass); 8 SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass); 9 //調用service層的分頁查詢 10 PageJson<OprPrintOrder> pagejson = new PageJson<OprPrintOrder>(service.list(queryable)); 11 //得到需要的結果集后的數據過濾操作 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中有數據查詢的要求 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就有了總頁數與總條數 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>

轉載于:https://www.cnblogs.com/guanghe/p/10026099.html

總結

以上是生活随笔為你收集整理的Mybatis-plus之RowBounds实现分页查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。