也许以下代码可能对您有用?
protected <T> List<T> queryPagingList(final String query, final PagingQueryContext context, final ParameterizedRowMapper<T> mapper, final SqlParameter... parameters) throws DataAccessException {
final Integer count = context.getCount();
final Integer beginIndex = context.getBeginIndex();
final List<SqlParameter> parameterList = Arrays.asList(parameters);
final PreparedStatementCreatorFactory preparedStatementCreatorFactory = new PreparedStatementCreatorFactory(query, parameterList);
preparedStatementCreatorFactory.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
preparedStatementCreatorFactory.setNativeJdbcExtractor(new NativeJdbcExtractorAdapter() {
@Override
public PreparedStatement getNativePreparedStatement(final PreparedStatement ps) throws SQLException {
ps.setFetchSize(count + 1);
ps.setMaxRows((beginIndex * count) + 1);
return ps;
}
@Override
public Statement getNativeStatement(final Statement stmt) throws SQLException {
stmt.setFetchSize(count + 1);
stmt.setMaxRows((beginIndex * count) + 1);
return stmt;
}
});
final PreparedStatementCreator psc = preparedStatementCreatorFactory.newPreparedStatementCreator(parameterList);
final ResultSetExtractor<List<T>> rse = new ResultSetExtractor<List<T>>() {
public List<T> extractData(final ResultSet rs) throws SQLException {
if (count > 0) {
rs.setFetchSize(count + 1);
if (beginIndex > 0) {
rs.absolute((beginIndex - 1) * count);
}
}
rs.setFetchDirection(ResultSet.FETCH_FORWARD);
final List<T> results = new ArrayList<T>(count + 1);
for (int rowNumber = 0; rs.next(); ++rowNumber) {
if (count > 0 && rowNumber > count) {
break;
}
results.add(mapper.mapRow(rs, rowNumber));
rs.last();
context.setTotalResults(rs.getRow());
}
return results;
}
};
return this.simpleJbcTemplate.query(psc, null, rse);
}
这是分页查询上下文:
public class PagingQueryContext implements Serializable {
private static final long serialVersionUID = -1887527330745224117L;
private Integer beginIndex = 0;
private Integer count = -1;
private Integer totalResults = -1;
public PagingQueryContext() {
}
public Integer getBeginIndex() {
return beginIndex;
}
public void setBeginIndex(final Integer beginIndex) {
this.beginIndex = beginIndex;
}
public Integer getCount() {
return count;
}
public void setCount(final Integer count) {
this.count = count;
}
public Integer getTotalResults() {
return totalResults;
}
public void setTotalResults(final Integer totalResults) {
this.totalResults = totalResults;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((beginIndex == null) ? 0 : beginIndex.hashCode());
result = prime * result + ((count == null) ? 0 : count.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof PagingQueryContext)) {
return false;
}
final PagingQueryContext other = (PagingQueryContext) obj;
if (beginIndex == null) {
if (other.beginIndex != null) {
return false;
}
} else if (!beginIndex.equals(other.beginIndex)) {
return false;
}
if (count == null) {
if (other.count != null) {
return false;
}
} else if (!count.equals(other.count)) {
return false;
}
return true;
}
}
它将获取大小加一,以便您可以查看是否会有更多结果。此外,根据您使用的 JDBC 驱动程序的实现方式rs.last()
,您可能不想在 the 中使用该调用ResultSetExtractor
并放弃 using totalRows
。某些驱动程序可能会在last()
调用时加载所有数据。