Mybatis 逻辑分页原理解析rowbounds

Mybatis提供了一个简单的逻辑分页使用类RowBounds(物理分页当然就是我们在sql语句中指定limit和offset值),在DefaultSqlSession提供的某些查询接口中我们可以看到RowBounds是作为参数用来进行分页的,如下接口:

[java]  view plain  copy
  1. public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds)  

RowBounds源码如下:

[java]  view plain  copy
  1. public class RowBounds {  
  2.   
  3.   /* 默认offset是0**/  
  4.   public static final int NO_ROW_OFFSET = 0;  
  5.     
  6.   /* 默认Limit是int的最大值,因此它使用的是逻辑分页**/  
  7.   public static final int NO_ROW_LIMIT = Integer.MAX_VALUE;  
  8.   public static final RowBounds DEFAULT = new RowBounds();  
  9.   
  10.   private int offset;  
  11.   private int limit;  
  12.   
  13.   public RowBounds() {  
  14.     this.offset = NO_ROW_OFFSET;  
  15.     this.limit = NO_ROW_LIMIT;  
  16.   }  
  17.   
  18.   public RowBounds(int offset, int limit) {  
  19.     this.offset = offset;  
  20.     this.limit = limit;  
  21.   }  
  22.   
  23.   public int getOffset() {  
  24.     return offset;  
  25.   }  
  26.   
  27.   public int getLimit() {  
  28.     return limit;  
  29.   }  
  30.   
  31. }  
逻辑分页的实现原理:

在DefaultResultSetHandler中,逻辑分页会将所有的结果都查询到,然后根据RowBounds中提供的offset和limit值来获取最后的结果,DefaultResultSetHandler实现如下:

[java]  view plain  copy
  1. private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping)  
  2.       throws SQLException {  
  3.     DefaultResultContext<Object> resultContext = new DefaultResultContext<Object>();  
  4.     //跳过RowBounds设置的offset值  
  5.     skipRows(rsw.getResultSet(), rowBounds);  
  6.     //判断数据是否小于limit,如果小于limit的话就不断的循环取值  
  7.     while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {  
  8.       ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);  
  9.       Object rowValue = getRowValue(rsw, discriminatedResultMap);  
  10.       storeObject(resultHandler, resultContext, rowValue, parentMapping, rsw.getResultSet());  
  11.     }  
  12.   }  
  13. private boolean shouldProcessMoreRows(ResultContext<?> context, RowBounds rowBounds) throws SQLException {  
  14.     //判断数据是否小于limit,小于返回true  
  15.     return !context.isStopped() && context.getResultCount() < rowBounds.getLimit();  
  16.   }  
  17.   //跳过不需要的行,应该就是rowbounds设置的limit和offset  
  18.   private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {  
  19.     if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {  
  20.       if (rowBounds.getOffset() != RowBounds.NO_ROW_OFFSET) {  
  21.         rs.absolute(rowBounds.getOffset());  
  22.       }  
  23.     } else {  
  24.       //跳过RowBounds中设置的offset条数据  
  25.       for (int i = 0; i < rowBounds.getOffset(); i++) {  
  26.         rs.next();  
  27.       }  
  28.     }  
  29.   }  

总结:Mybatis的逻辑分页比较简单,简单来说就是取出所有满足条件的数据,然后舍弃掉前面offset条数据,然后再取剩下的数据的limit条

猜你喜欢

转载自blog.csdn.net/suchahaerkang/article/details/80258500