/**
*
* <p>Title: 分页公共类</p>
* <p>Description: 使用该函数时需要设置页大小,当前页,然后调用initPage方法,得到一个PageUtil实例</p>
*/
public class Pagination extends ArrayList {
//首页
private int firstPage = 1;
//最后一页
private int lastPage = 1;
//下一页
private int nextPage = 0;
//前一页
private int prePage = 0;
//记录数
private int recordCount = 0;
//总页数
private int pageCount = 0;
//当前页
private int currentPage = 1;
//一页显示条数
private int pageSize = 0;
//每页的第一笔数据
private int startIndex = 1;
//每页的最后一笔数据
private int lastIndex = 1;
//总共记录数
private int totalRecord = 0;
public Pagination() {
}
public Pagination(int currentPage, int pageSize) {
if (currentPage > 0) {
this.currentPage = currentPage;
}
this.pageSize = pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getFirstPage() {
return firstPage;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
public int getLastPage() {
return lastPage;
}
public void setLastPage(int lastPage) {
this.lastPage = lastPage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getPrePage() {
return prePage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getRecordCount() {
return recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
public void initPage(int total) {
//得到记录数
recordCount = total;
if (pageSize != 0 && recordCount != 0) {
//得到总页数
pageCount = (int) Math.ceil( (float) recordCount / pageSize);
} else {
pageCount = 1;
}
//得到最后一页
lastPage = pageCount;
//得到上一页
if ( (currentPage - 1) >= firstPage) {
prePage = currentPage - 1;
} else {
prePage = currentPage;
}
//得到下一页
if ( (currentPage + 1) <= lastPage) {
nextPage = currentPage + 1;
} else {
nextPage = currentPage;
}
//得到每页的第一笔数
startIndex = (currentPage - 1) * pageSize + 1;
//得到每页的最后一笔数
lastIndex = currentPage * pageSize;
if (lastIndex > recordCount) {
lastIndex = recordCount;
}
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
//设置总记录数
this.totalRecord = totalRecord;
//初始化各种数据
this.initPage(this.totalRecord);
}
public void setLastIndex(int lastIndex) {
this.lastIndex = lastIndex;
}
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
public int getStartIndex() {
return startIndex;
}
public int getLastIndex() {
return lastIndex;
}
}
调用的时候:
public List doFindObjectsBySQL(String sql,
RowCallbackHandler rcHandler,
int pageNum, int pageSize) {
JdbcTemplate jt = new JdbcTemplate(dataSource);
Pagination pagination = new Pagination(pageNum, pageSize);
int total = doCountObjectsBySQL(sql, jt);
pagination.setTotalRecord(total);
if (total > 0) {
StringBuffer paginationSQL = new StringBuffer(" SELECT * FROM ( ");
paginationSQL.append(" SELECT temp.* ,ROWNUM num FROM ( ");
paginationSQL.append(sql);
paginationSQL.append(" ) temp where ROWNUM <= " +
pagination.getLastIndex());
paginationSQL.append(" ) WHERE num > " + pagination.getStartIndex());
pagination.addAll(jt.query(paginationSQL.toString(), rcHandler));
}
return pagination;
}