在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() 分页处理的函数如下
private void handleResults(RequestScope request, ResultSet rs, int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException { try { request.setResultSet(rs); ResultMap resultMap = request.getResultMap(); if (resultMap != null) { // Skip Results if (rs.getType() != ResultSet.TYPE_FORWARD_ONLY) { if (skipResults > 0) { rs.absolute(skipResults); } } else { for (int i = 0; i < skipResults; i++) { if (!rs.next()) { return; } } } // Get Results int resultsFetched = 0; while ((maxResults == SqlExecutor.NO_MAXIMUM_RESULTS || resultsFetched < maxResults) && rs.next()) { Object[] columnValues = resultMap.resolveSubMap(request, rs).getResults(request, rs); callback.handleResultObject(request, columnValues, rs); resultsFetched++; } } } finally { request.setResultSet(null); } } 返回的PaginatedList实际上是PaginatedDataList类的对象,每次翻页的时候最后都会调用
private List getList(int idx, int localPageSize) throws SQLException { return sqlMapExecutor.queryForList(statementName, parameterObject, (idx) * pageSize, localPageSize); } 这个方法,可见ibatis的分页机制要看jdbcDriver如何实现以及是否支持rs.absolute(skipResults)。 这种实现肯定不如数据库自己支持的分页方式来的快,一旦碰到数据量大的表,马上会死翘翘。 |
|