物理分頁和邏輯分頁
物理分頁:直接從數據庫中拿出我們需要的數據,例如在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>