iBATIS分页源码真相探讨

来源:互联网 发布:mysql并联 编辑:程序博客网 时间:2024/05/01 08:59

iBATIS分页源码真相的探讨,首先我们在iBATIS中有一个很吸引人的方法,queryForPaginatedList(java.lang.String id, int pageSize),可以返回 PaginatedList的对象,实现翻页,刚才测试了一下PaginatedList,在1-2w行数据的时候还可以工作,但是在一个30w行的表里翻页,一次select用了363.031second忍不住看了一下源,发现iBATIS的分页依赖于数据库的JDBC Driver.

调用次序如下SqlMapClientImpl.queryForPaginatedList->SqlMapSessionImpl.queryForPaginatedList

->SqlMapExecutorDelegate.queryForPaginatedList->GeneralStatement.executeQueryForList

->GeneralStatment.executeQueryWithCallback->GeneralStatment.executeQueryWithCallback

->SqlExecutor.executeQuery->SqlExecutor.handleMultipleResults()

iBATIS分页处理的函数如下

  1. private void handleResults(RequestScope request, ResultSet rs, int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException {  
  2.     try {  
  3.       request.setResultSet(rs);  
  4.       ResultMap resultMap = request.getResultMap();  
  5.       if (resultMap != null) {  
  6.         // Skip Results  
  7.         if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {  
  8.           if (skipResults > 0) {   
  9.  
  10.  
  11.             rs.absolute(skipResults);  
  12.           }  
  13.         } else {  
  14.           for (int i = 0; i < skipResults; i++) {  
  15.             if (!rs.next()) {  
  16.               return;  
  17.             }  
  18.           }  
  19.         }   
  20.  
  21.  
  22.         // Get Results  
  23.         int resultsFetched = 0;  
  24.         while ((maxResults == SqlExecutor.NO_MAXIMUM_RESULTS || resultsFetched < maxResults) && rs.next()) {  
  25.           Object[] columnValues = resultMap.resolveSubMap(request, rs).getResults(request, rs);  
  26.           callback.handleResultObject(request, columnValues, rs);  
  27.           resultsFetched++;  
  28.         }  
  29.       }  
  30.     } finally {  
  31.       request.setResultSet(null);  
  32.     }  
  33.   } 

返回的PaginatedList实际上是PaginatedDataList类的对象,每次翻页的时候最后都会调用

  1. private List getList(int idx, int localPageSize) throws SQLException {   
  2.    return sqlMapExecutor.queryForList(statementName, parameterObject, (idx) * pageSize, localPageSize);  
  3.  } 

这个方法,可见iBATIS的分页机制要看JDBC Driver如何实现以及是否支持rs.absolute(skipResults)。

这种实现肯定不如数据库自己支持的分页方式来的快,一旦碰到数据量大的表,有停滞的可能。

iBATIS分页的源码分析就到这里,希望你能对iBATIS分页的根本有所了解。

出处:http://developer.51cto.com/art/200907/137212.htm