用Hibernate怎么对数据进行分页
展开全部
public class Page {
public static final int DEFAULT_PAGE_SIZE = 10;
private int pageIndex; //当前第几页
private int pageSize; //一页显示几条
private int totalCount; //总共多少条
private int pageCount; //总共多少页
public Page(int pageIndex, int pageSize) {
if(pageIndex<1)
pageIndex = 1;
if(pageSize<咐携1)
pageSize = 1;
this.pageIndex = pageIndex;
this.pageSize = pageSize;
}
public Page(int pageIndex) {
this(pageIndex, DEFAULT_PAGE_SIZE);
}
public int getPageIndex() { return pageIndex; }
public int getPageSize() { return pageSize; }
public int getPageCount() { return pageCount; }
public int getTotalCount() { return totalCount; }
public int getFirstResult() { return (pageIndex-1)*pageSize; }
public boolean getHasPrevious() { return pageIndex>桥简让1; }
public boolean getHasNext() { return pageIndex<pageCount; }
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
pageCount = totalCount / pageSize + (totalCount%pageSize==0 ? 0 : 1);
if(totalCount==0) {
if(pageIndex!=1)
throw new IndexOutOfBoundsException("Page index out of range."敏局);
}
else {
if(pageIndex>pageCount)
throw new IndexOutOfBoundsException("Page index out of range.");
}
}
public boolean isEmpty() {
return totalCount==0;
}
}
page 类
protected List queryForList(final String select, final Object[] values, final Page page) {
HibernateCallback selectCallback = new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(select);
if(values!=null) {
for(int i=0; i<values.length; i++)
query.setParameter(i, values[i]);
}
return query.setFirstResult(page.getFirstResult())
.setMaxResults(page.getPageSize())
.list();
}
};
return (List) hibernateTemplate.executeFind(selectCallback);
}
根据page查询
public static final int DEFAULT_PAGE_SIZE = 10;
private int pageIndex; //当前第几页
private int pageSize; //一页显示几条
private int totalCount; //总共多少条
private int pageCount; //总共多少页
public Page(int pageIndex, int pageSize) {
if(pageIndex<1)
pageIndex = 1;
if(pageSize<咐携1)
pageSize = 1;
this.pageIndex = pageIndex;
this.pageSize = pageSize;
}
public Page(int pageIndex) {
this(pageIndex, DEFAULT_PAGE_SIZE);
}
public int getPageIndex() { return pageIndex; }
public int getPageSize() { return pageSize; }
public int getPageCount() { return pageCount; }
public int getTotalCount() { return totalCount; }
public int getFirstResult() { return (pageIndex-1)*pageSize; }
public boolean getHasPrevious() { return pageIndex>桥简让1; }
public boolean getHasNext() { return pageIndex<pageCount; }
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
pageCount = totalCount / pageSize + (totalCount%pageSize==0 ? 0 : 1);
if(totalCount==0) {
if(pageIndex!=1)
throw new IndexOutOfBoundsException("Page index out of range."敏局);
}
else {
if(pageIndex>pageCount)
throw new IndexOutOfBoundsException("Page index out of range.");
}
}
public boolean isEmpty() {
return totalCount==0;
}
}
page 类
protected List queryForList(final String select, final Object[] values, final Page page) {
HibernateCallback selectCallback = new HibernateCallback() {
public Object doInHibernate(Session session) {
Query query = session.createQuery(select);
if(values!=null) {
for(int i=0; i<values.length; i++)
query.setParameter(i, values[i]);
}
return query.setFirstResult(page.getFirstResult())
.setMaxResults(page.getPageSize())
.list();
}
};
return (List) hibernateTemplate.executeFind(selectCallback);
}
根据page查询
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询