1.The Pager.java
2.按属性分页查询
package com.music.util.pager;
import java.util.ArrayList;
import java.util.List;
//分页 戚拂晓 2011.2.24
public class Pager {
//页面大小
private Integer[] pageSizeList={10,15,25,50,100,200,300,500};
//一页显示的记录数
private Integer pageSize = 25;
//当前页码
private Integer pageNo= 1;
//记录总数
private Integer rowCount=0;
//总页数
private Integer pageCount=1;
//起始行数
private Integer startIndex = 1;
//结束行数
private Integer endIndex=1;
private Integer firstPageNo=1;
private Integer prePageNo=1;
private Integer nextPageNo=1;
private Integer lastPageNo=1;
@SuppressWarnings("unchecked")
private List resultList;
@SuppressWarnings("unchecked")
public Pager(Integer pageSize,Integer pageNo,Integer rowCount,List resultList){
super();
this.pageSize = pageSize;
this.pageNo = pageNo;
this.rowCount = rowCount;
this.resultList = resultList;
if(rowCount%pageSize==0)
this.pageCount = rowCount/pageSize;
else
this.pageCount = rowCount/pageSize + 1;
this.startIndex = pageSize*(pageNo-1);
this.endIndex = this.startIndex + resultList.size();
this.lastPageNo = this.pageCount;
if(this.lastPageNo==0){
this.lastPageNo=1;
}
if(this.pageNo>1)
this.prePageNo = this.pageNo-1;
if(this.pageNo==this.lastPageNo)
this.nextPageNo = this.lastPageNo;
else this.nextPageNo=this.pageNo+1;
}
//输出每页显示记录数的数值列表
@SuppressWarnings("unchecked")
public Object[] getPageSizeIndexs(){
List result = new ArrayList(pageSizeList.length);
for(Integer i=0;i<pageSizeList.length;i++){
result.add(String.valueOf(pageSizeList[i]));
}
Object[] indexs = (result.toArray());
return indexs;
}
//用于输出页码数值列表
@SuppressWarnings("unchecked")
public Object[] getPageNoIndexs(){
List result = new ArrayList(pageCount);
for(Integer i=0;i<pageCount;i++){
result.add(String.valueOf(i+1));
}
Object[] indexs = (result.toArray());
return indexs;
}
public Integer[] getPageSizeList() {
return pageSizeList;
}
public void setPageSizeList(Integer[] pageSizeList) {
this.pageSizeList = pageSizeList;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getPageNo() {
return pageNo;
}
public void setPageNo(Integer pageNo) {
this.pageNo = pageNo;
}
public Integer getRowCount() {
return rowCount;
}
public void setRowCount(Integer rowCount) {
this.rowCount = rowCount;
}
public Integer getPageCount() {
return pageCount;
}
public void setPageCount(Integer pageCount) {
this.pageCount = pageCount;
}
public Integer getStartIndex() {
return startIndex;
}
public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
}
public Integer getEndIndex() {
return endIndex;
}
public void setEndIndex(Integer endIndex) {
this.endIndex = endIndex;
}
public Integer getFirstPageNo() {
return firstPageNo;
}
public void setFirstPageNo(Integer firstPageNo) {
this.firstPageNo = firstPageNo;
}
public Integer getPrePageNo() {
return prePageNo;
}
public void setPrePageNo(Integer prePageNo) {
this.prePageNo = prePageNo;
}
public Integer getNextPageNo() {
return nextPageNo;
}
public void setNextPageNo(Integer nextPageNo) {
this.nextPageNo = nextPageNo;
}
public Integer getLastPageNo() {
return lastPageNo;
}
public void setLastPageNo(Integer lastPageNo) {
this.lastPageNo = lastPageNo;
}
@SuppressWarnings("unchecked")
public List getResultList() {
return resultList;
}
@SuppressWarnings("unchecked")
public void setResultList(List resultList) {
this.resultList = resultList;
}
}
2.按属性分页查询
@SuppressWarnings("unchecked")
public Pager pageLoad(final Integer pageNo, final Integer pageSize) {
int rowCount = 0;
Number number = (Number) this.hibernateTemplate.iterate("select count(*) from WebArticle").next();
rowCount = number.intValue();
System.out.println(pageNo);
System.out.println(rowCount);
List log = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int startIndex = pageSize * (pageNo-1);
Query query = session.createQuery("from WebArticle order by updatetime desc");
query.setFirstResult(startIndex);
query.setMaxResults(pageSize);
List result = query.list();
return result;
}
});
return new Pager(pageSize, pageNo, rowCount, log);
}