基于hibernate实现的分页技术实例分析
本文实例讲述了基于hibernate实现的分页技术。分享给大家供大家参考,具体如下:
先说明一下基于hibernate实现分页的原理,假如从数据库取出100条数据,我们要让每页显示10条,假如从30开始,只需要设置起始位置和最大的返回结果即可
先上代码:注意传进来的参数有Page这类,后面有介绍
publicList<Article>queryByPage(finalStringusername,finalPagepage){
returnthis.getHibernateTemplate().executeFind(newHibernateCallback(){
publicObjectdoInHibernate(Sessionsession)
throwsHibernateException,SQLException{
Queryquery=session.createQuery("selectartfromArticleartwhereart.username=?");
//设置参数
query.setParameter(0,username);
//设置每页显示多少个,设置多大结果。
query.setMaxResults(page.getEveryPage());
//设置起点
query.setFirstResult(page.getBeginIndex());
returnquery.list();
}
});
上面关键代码是setMaxResults(),和setFirstResult(),即设置最大显示值和起点
这里我们需要一个Page工具类,用来操作分页。
Page.java:
packagecom.fenye;
publicclassPage{
//1.每页显示数量(everyPage)
privateinteveryPage;
//2.总记录数(totalCount)
privateinttotalCount;
//3.总页数(totalPage)
privateinttotalPage;
//4.当前页(currentPage)
privateintcurrentPage;
//5.起始点(beginIndex)
privateintbeginIndex;
//6.是否有上一页(hasPrePage)
privatebooleanhasPrePage;
//7.是否有下一页(hasNextPage)
privatebooleanhasNextPage;
publicPage(inteveryPage,inttotalCount,inttotalPage,intcurrentPage,
intbeginIndex,booleanhasPrePage,booleanhasNextPage){
this.everyPage=everyPage;
this.totalCount=totalCount;
this.totalPage=totalPage;
this.currentPage=currentPage;
this.beginIndex=beginIndex;
this.hasPrePage=hasPrePage;
this.hasNextPage=hasNextPage;
}
//构造函数,默认
publicPage(){}
//构造方法,对所有属性进行设置
publicintgetEveryPage(){
returneveryPage;
}
publicvoidsetEveryPage(inteveryPage){
this.everyPage=everyPage;
}
publicintgetTotalCount(){
returntotalCount;
}
publicvoidsetTotalCount(inttotalCount){
this.totalCount=totalCount;
}
publicintgetTotalPage(){
returntotalPage;
}
publicvoidsetTotalPage(inttotalPage){
this.totalPage=totalPage;
}
publicintgetCurrentPage(){
returncurrentPage;
}
publicvoidsetCurrentPage(intcurrentPage){
this.currentPage=currentPage;
}
publicintgetBeginIndex(){
returnbeginIndex;
}
publicvoidsetBeginIndex(intbeginIndex){
this.beginIndex=beginIndex;
}
publicbooleanisHasPrePage(){
returnhasPrePage;
}
publicvoidsetHasPrePage(booleanhasPrePage){
this.hasPrePage=hasPrePage;
}
publicbooleanisHasNextPage(){
returnhasNextPage;
}
publicvoidsetHasNextPage(booleanhasNextPage){
this.hasNextPage=hasNextPage;
}
}
Page工具类主要是封装页面信息,一共多少数据啊,一页显示多少啊,起点的序号,总页数,是否有上一页下一页,当前页。
还需要一个操作page的工具类,PageUtil.java
packagecom.sanqing.fenye;
/*
*分页信息辅助类
*/
publicclassPageUtil{
publicstaticPagecreatePage(inteveryPage,inttotalCount,intcurrentPage){
everyPage=getEveryPage(everyPage);
currentPage=getCurrentPage(currentPage);
inttotalPage=getTotalPage(everyPage,totalCount);
intbeginIndex=getBeginIndex(everyPage,currentPage);
booleanhasPrePage=getHasPrePage(currentPage);
booleanhasNextPage=getHasNextPage(totalPage,currentPage);
returnnewPage(everyPage,totalCount,totalPage,currentPage,
beginIndex,hasPrePage,hasNextPage);
}
publicstaticPagecreatePage(Pagepage,inttotalCount){
inteveryPage=getEveryPage(page.getEveryPage());
intcurrentPage=getCurrentPage(page.getCurrentPage());
inttotalPage=getTotalPage(everyPage,totalCount);
intbeginIndex=getBeginIndex(everyPage,currentPage);
booleanhasPrePage=getHasPrePage(currentPage);
booleanhasNextPage=getHasNextPage(totalPage,currentPage);
returnnewPage(everyPage,totalCount,totalPage,currentPage,
beginIndex,hasPrePage,hasNextPage);
}
//设置每页显示记录数
publicstaticintgetEveryPage(inteveryPage){
returneveryPage==0?10:everyPage;
}
//设置当前页
publicstaticintgetCurrentPage(intcurrentPage){
returncurrentPage==0?1:currentPage;
}
//设置总页数,需要总记录数,每页显示多少
publicstaticintgetTotalPage(inteveryPage,inttotalCount){
inttotalPage=0;
if(totalCount%everyPage==0){
totalPage=totalCount/everyPage;
}else{
totalPage=totalCount/everyPage+1;
}
returntotalPage;
}
//设置起始点,需要每页显示多少,当前页
publicstaticintgetBeginIndex(inteveryPage,intcurrentPage){
return(currentPage-1)*everyPage;
}
//设置是否有上一页,需要当前页
publicstaticbooleangetHasPrePage(intcurrentPage){
returncurrentPage==1?false:true;
}
//设置是否有下一个,需要总页数和当前页
publicstaticbooleangetHasNextPage(inttotalPage,intcurrentPage){
returncurrentPage==totalPage||totalPage==0?false:true;
}
}
创建Page只需要3个参数,每页显示多少数据,当前页,总共多少数据,其他的4个参数都可以通过这三个计算出来
所以后面要创建Page,只需要调用这工具方法PageUtil.createPage(3个参数),就返回一Page.
返回的Page就是前面参数的Page,即要显示的分页
这样就算完成了分页的功能。
希望本文所述对大家基于Hibernate框架的Java程序设计有所帮助。