Java tutorial
/* * (C) 2007-2012 Alibaba Group Holding Limited. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * Authors: * leiwen <chrisredfield1985@126.com> , boyan <killme2008@gmail.com> */ package com.starit.diamond.server.utils; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.ResultSetExtractor; import org.springframework.jdbc.core.simple.ParameterizedRowMapper; import com.starit.diamond.domain.Page; /** * * * @author boyan * @date 2010-5-6 * @param <E> */ public class PaginationHelper<E> { /** * ? * * @param jt * jdbcTemplate * @param sqlCountRows * SQL * @param sqlFetchRows * ?sql * @param args * ? * @param pageNo * * @param pageSize * ?? * @param rowMapper * @return */ public Page<E> fetchPage(final JdbcTemplate jt, final String sqlCountRows, final String sqlFetchRows, final Object args[], final int pageNo, final int pageSize, final ParameterizedRowMapper<E> rowMapper) { if (pageSize == 0) { return null; } // ? final int rowCount = jt.queryForInt(sqlCountRows, args); // int pageCount = rowCount / pageSize; if (rowCount > pageSize * pageCount) { pageCount++; } // Page final Page<E> page = new Page<E>(); page.setPageNumber(pageNo); page.setPagesAvailable(pageCount); page.setTotalCount(rowCount); if (pageNo > pageCount) return null; // ???? final int startRow = (pageNo - 1) * pageSize; // TODO ?? limit String selectSQL = ""; if ("mysql".equals(ProfileUtil.getDbType())) selectSQL = sqlFetchRows + " limit " + startRow + "," + pageSize; else if ("oracle".equals(ProfileUtil.getDbType())) selectSQL = "SELECT * FROM (SELECT A.*, ROWNUM RN FROM (" + (sqlFetchRows) + ") A WHERE ROWNUM <= " + (pageSize + startRow) + ") WHERE RN >= " + (startRow + 1); jt.query(selectSQL, args, new ResultSetExtractor() { public Object extractData(ResultSet rs) throws SQLException, DataAccessException { final List<E> pageItems = page.getPageItems(); int currentRow = 0; while (rs.next()) { pageItems.add(rowMapper.mapRow(rs, currentRow++)); } return page; } }); return page; } }