分享

ibatis中queryForPaginatedList分页 源码分析

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

调用次序如下SqlMapClientImpl.queryForPaginatedList->SqlMapSessionImpl.queryForPaginatedList
->SqlMapExecutorDelegate.queryForPaginatedList->GeneralStatement.executeQueryForList
->GeneralStatment.executeQueryWithCallback->GeneralStatment.executeQueryWithCallback
->SqlExecutor.executeQuery->SqlExecutor.handleMultipleResults()
分页处理的函数如下
Java代码 复制代码
  1. private void handleResults(RequestScope request, ResultSet rs, int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException {   
  2.   
  3.     try {   
  4.   
  5.       request.setResultSet(rs);   
  6.   
  7.       ResultMap resultMap = request.getResultMap();   
  8.   
  9.       if (resultMap != null) {   
  10.   
  11.         // Skip Results   
  12.   
  13.         if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) {   
  14.   
  15.           if (skipResults > 0) {   
  16.   
  17.             rs.absolute(skipResults);   
  18.   
  19.           }   
  20.   
  21.         } else {   
  22.   
  23.           for (int i = 0; i < skipResults; i++) {   
  24.   
  25.             if (!rs.next()) {   
  26.   
  27.               return;   
  28.   
  29.             }   
  30.   
  31.           }   
  32.   
  33.         }   
  34.   
  35. // Get Results   
  36. int resultsFetched = 0;   
  37. while ((maxResults == SqlExecutor.NO_MAXIMUM_RESULTS || resultsFetched < maxResults) && rs.next()) {   
  38. Object[] columnValues = resultMap.resolveSubMap(request, rs).getResults(request, rs);   
  39. callback.handleResultObject(request, columnValues, rs);   
  40. resultsFetched++;   
  41. }   
  42. }   
  43. finally {   
  44. request.setResultSet(null);   
  45. }   
  46. }   

返回的PaginatedList实际上是PaginatedDataList类的对象,每次翻页的时候最后都会调用
Java代码 复制代码
  1. private List getList(int idx, int localPageSize) throws SQLException {   
  2.   
  3.     return sqlMapExecutor.queryForList(statementName, parameterObject, (idx) * pageSize, localPageSize);   
  4.   
  5.   }  

这个方法,可见ibatis的分页机制要看jdbcDriver如何实现以及是否支持rs.absolute(skipResults)。
这种实现肯定不如数据库自己支持的分页方式来的快,一旦碰到数据量大的表,马上会死翘翘。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多