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()
iBATIS分页处理的函数如下
Java代码
- 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类的对象,每次翻页的时候最后都会调用
Java代码
- private List getList(int idx, int localPageSize); throws SQLException {
- return sqlMapExecutor.queryForList(statementName, parameterObject, (idx); * pageSize, localPageSize);;
- }
这个方法,可见iBATIS的分页机制要看jdbcDriver如何实现以及是否支持rs.absolute(skipResults)。
iBATIS分页的情况就介绍到这里,希望对你有所帮助。
【编辑推荐】