packagecom.zyz.dao.base;/*** Created by zyz on 2016-8-11.*/
importjava.io.Serializable;importjava.util.List;importorg.springframework.jdbc.core.JdbcTemplate;public class PageResult implementsSerializable {public static final int NUMBERS_PER_PAGE = 10;//默认每页10条记录//每页显示的记录数
private intnumPerPage;//记录总数
private inttotalRows;//总页数
private inttotalPages;//当前页码
private intcurrentPage;//起始行数
private intstartIndex;//结束行数
private intlastIndex;//结果集存放List
private ListresultList;//JdbcTemplate jTemplate
privateJdbcTemplate jdbcTemplate;/*** 每页显示10条记录的构造函数,使用该函数必须先给Pagination设置currentPage,jTemplate初值
*@paramsql oracle语句*/
publicPageResult(String sql){if(jdbcTemplate == null){throw new IllegalArgumentException("com.itbegin.dao.base.PageResult.jTemplate is null,please initial it first. ");
}else if(sql.equals("")){throw new IllegalArgumentException("com.itbegin.dao.base.PageResult.sql is empty,please initial it first. ");
}new PageResult(sql,currentPage,NUMBERS_PER_PAGE,jdbcTemplate);
}/**分页构造函数
*@paramsql 根据传入的sql语句得到一些基本分页信息
*@paramcurrentPage 当前页
*@paramnumPerPage 每页记录数
*@paramjTemplate JdbcTemplate实例*/
public PageResult(String sql,int currentPage,intnumPerPage,JdbcTemplate jTemplate){if(jTemplate == null){throw new IllegalArgumentException("com.deity.ranking.util.Pagination.jTemplate is null,please initial it first. ");
}else if(sql == null || sql.equals("")){throw new IllegalArgumentException("com.deity.ranking.util.Pagination.sql is empty,please initial it first. ");
}//设置每页显示记录数
setNumPerPage(numPerPage);//设置要显示的页数
setCurrentPage(currentPage);//计算总记录数
StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
totalSQL.append(sql);
totalSQL.append(" ) totalTable ");//给JdbcTemplate赋值
this.jdbcTemplate =jTemplate;//总记录数
setTotalRows(getJdbcTemplate().queryForObject(totalSQL.toString(),Integer.class));//计算总页数
setTotalPages();//计算起始行数
setStartIndex();//计算结束行数
setLastIndex();//装入结果集
setResultList(getJdbcTemplate().queryForList(getMySQLPageSQL(sql,startIndex,numPerPage)));
}/*** 构造MySQL数据分页SQL
*@paramqueryString
*@paramstartIndex
*@parampageSize
*@return
*/
publicString getMySQLPageSQL(String queryString, Integer startIndex, Integer pageSize)
{
String result= "";if (null != startIndex && null !=pageSize)
{
result= queryString + " limit " + startIndex + "," +pageSize;
}else if (null != startIndex && null ==pageSize)
{
result= queryString + " limit " +startIndex;
}else{
result=queryString;
}returnresult;
}public intgetCurrentPage() {returncurrentPage;
}public void setCurrentPage(intcurrentPage) {this.currentPage =currentPage;
}public intgetNumPerPage() {returnnumPerPage;
}public void setNumPerPage(intnumPerPage) {this.numPerPage =numPerPage;
}public ListgetResultList() {returnresultList;
}public voidsetResultList(List resultList) {this.resultList =resultList;
}public intgetTotalPages() {returntotalPages;
}//计算总页数
public voidsetTotalPages() {if(totalRows % numPerPage == 0){this.totalPages = totalRows /numPerPage;
}else{this.totalPages = (totalRows / numPerPage) + 1;
}
}public intgetTotalRows() {returntotalRows;
}public void setTotalRows(inttotalRows) {this.totalRows =totalRows;
}public intgetStartIndex() {returnstartIndex;
}public voidsetStartIndex() {this.startIndex = (currentPage - 1) *numPerPage;
}public intgetLastIndex() {returnlastIndex;
}publicJdbcTemplate getJdbcTemplate() {returnjdbcTemplate;
}public voidsetJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate =jdbcTemplate;
}//计算结束时候的索引
public voidsetLastIndex() {//System.out.println("totalRows="+totalRows);/System.out.println("numPerPage="+numPerPage);///
if( totalRows
}else if((totalRows % numPerPage == 0) || (totalRows % numPerPage != 0 && currentPage
}else if(totalRows % numPerPage != 0 && currentPage == totalPages){//最后一页
this.lastIndex =totalRows ;
}
}
@OverridepublicString toString() {return "totalRows:"+totalRows+" totalPages:"+totalPages+" currentPage:"+currentPage+resultList;
}
}