ibatis 两次查询的问题
10年前的项目框架中发现分页检索都是两次查询,执行的线程是相同的
一开始怀疑是 DAO 和 DAO2 中有重名的方法导致
后来经过反复调查,原来是在DAO中使用 queryForPaginatedList 导致的
com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl
/** * Execute a query and return a paginated list * * @param sessionScope - the session scope * @param id - the statement ID * @param paramObject - the parameter object * @param pageSize - the page size * @return - the data list * @throws SQLException - if the query fails * @deprecated All paginated list features have been deprecated */ public PaginatedList queryForPaginatedList(SessionScope sessionScope, String id, Object paramObject, int pageSize) throws SQLException { return new PaginatedDataList(sessionScope.getSqlMapExecutor(), id, paramObject, pageSize); }
上面这个执行一次查询,紧接着gotoPage 会执行第二次查询
/** * Moves to a specified page. If the specified * page is beyond the last page, wrap to the first page. * If the specified page is before the first page, wrap * to the last page. * * @param pageNumber The page to go to */ public void gotoPage(int pageNumber);
上面这个已经不推荐适用了,修改为即可。
/** * Execute a query for a list * * @param sessionScope - the session scope * @param id - the statement ID * @param paramObject - the parameter object * @param skip - the number of rows to skip * @param max - the maximum number of rows to return * @return - the data list * @throws SQLException - if the query fails */ public List queryForList(SessionScope sessionScope, String id, Object paramObject, int skip, int max) throws SQLException { List list = null; MappedStatement ms = getMappedStatement(id); Transaction trans = getTransaction(sessionScope); boolean autoStart = trans == null; try { trans = autoStartTransaction(sessionScope, autoStart, trans); StatementScope statementScope = beginStatementScope(sessionScope, ms); try { list = ms.executeQueryForList(statementScope, trans, paramObject, skip, max); } finally { endStatementScope(statementScope); } autoCommitTransaction(sessionScope, autoStart); } finally { autoEndTransaction(sessionScope, autoStart); } return list; }