利用hibernate的DetachedCriteria进行分页

Hibernate3提供了DetachedCriteria,使得我们可以在Web层构造detachedCriteria,然后调用业务层Bean,进行动态条件查询,根据这一功能,我设计了通用的抽象Bean基类和分页类支持,代码来自于QuakeWang的javaeye-core包的相应类,然后又做了很多修改。

分页支持类:

importjava.util.List;

publicclassPaginationSupport{

publicfinalstaticintPAGESIZE=30;//定义一页显示多少行记录

privateintpageSize=PAGESIZE;

privateListitems;//查询的结果集

privateinttotalCount;//一共有多少条适合的记录

privateint[]indexes=newint[0];//查出总记录数后,记录每一页开始的记录号

privateintstartIndex=0;//当前为第几页

/*------------------------------三个构造函数*/

publicPaginationSupport(Listitems,inttotalCount){

setPageSize(PAGESIZE);

setTotalCount(totalCount);

setItems(items);

setStartIndex(0);

}

publicPaginationSupport(Listitems,inttotalCount,intstartIndex){

setPageSize(PAGESIZE);

setTotalCount(totalCount);

setItems(items);

setStartIndex(startIndex);

}

publicPaginationSupport(Listitems,inttotalCount,intpageSize,intstartIndex){

setPageSize(pageSize);

setTotalCount(totalCount);

setItems(items);

setStartIndex(startIndex);

}

publicListgetItems(){

returnitems;

}

publicvoidsetItems(Listitems){

this.items=items;

}

publicintgetPageSize(){

returnpageSize;

}

publicvoidsetPageSize(intpageSize){

this.pageSize=pageSize;

}

publicintgetTotalCount(){

returntotalCount;

}

//设置总的记录数,总页数(count),每一页的开始记录号

publicvoidsetTotalCount(inttotalCount){

if(totalCount>0){

this.totalCount=totalCount;

intcount=totalCount/pageSize;

if(totalCount%pageSize>0)

count++;

indexes=newint[count];

for(inti=0;i<count;i++){

indexes[i]=pageSize*i;

}

}else{

this.totalCount=0;

}

}

publicint[]getIndexes(){

returnindexes;

}

publicvoidsetIndexes(int[]indexes){

this.indexes=indexes;

}

publicintgetStartIndex(){

returnstartIndex;

}

//设置起始页的位置

publicvoidsetStartIndex(intstartIndex){

if(totalCount<=0)

this.startIndex=0;

elseif(startIndex>=totalCount)

this.startIndex=indexes[indexes.length-1];

elseif(startIndex<0)

this.startIndex=0;

else{

this.startIndex=indexes[startIndex/pageSize];

}

}

//下一页

publicintgetNextIndex(){

intnextIndex=getStartIndex()+pageSize;

if(nextIndex>=totalCount)

returngetStartIndex();

else

returnnextIndex;

}

//上一页

publicintgetPreviousIndex(){

intpreviousIndex=getStartIndex()-pageSize;

if(previousIndex<0)

return0;

else

returnpreviousIndex;

}

}

抽象业务类(我们的类要继承这个抽象类,以便使用里面的类进行分类)

importjava.io.Serializable;

importjava.util.List;

importorg.hibernate.Criteria;

importorg.hibernate.HibernateException;

importorg.hibernate.Session;

importorg.hibernate.criterion.DetachedCriteria;

importorg.hibernate.criterion.Projections;

importorg.springframework.orm.hibernate3.HibernateCallback;

importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;

publicabstractclassAbstractManagerextendsHibernateDaoSupport{

privatebooleancacheQueries=false;

privateStringqueryCacheRegion;

publicvoidsetCacheQueries(booleancacheQueries){

this.cacheQueries=cacheQueries;

}

publicvoidsetQueryCacheRegion(StringqueryCacheRegion){

this.queryCacheRegion=queryCacheRegion;

}

//保存

publicvoidsave(finalObjectentity){

getHibernateTemplate().save(entity);

}

publicvoidpersist(finalObjectentity){

getHibernateTemplate().save(entity);

}

//更新

publicvoidupdate(finalObjectentity){

getHibernateTemplate().update(entity);

}

//删除

publicvoiddelete(finalObjectentity){

getHibernateTemplate().delete(entity);

}

//加载

publicObjectload(finalClassentity,finalSerializableid){

returngetHibernateTemplate().load(entity,id);

}

publicObjectget(finalClassentity,finalSerializableid){

returngetHibernateTemplate().get(entity,id);

}

//查询全部

publicListfindAll(finalClassentity){

returngetHibernateTemplate().find("from"+entity.getName());

}

//通过名字查询

publicListfindByNamedQuery(finalStringnamedQuery){

returngetHibernateTemplate().findByNamedQuery(namedQuery);

}

//通过参数动态查询

publicListfindByNamedQuery(finalStringquery,finalObjectparameter){

returngetHibernateTemplate().findByNamedQuery(query,parameter);

}

publicListfindByNamedQuery(finalStringquery,finalObject[]parameters){

returngetHibernateTemplate().findByNamedQuery(query,parameters);

}

publicListfind(finalStringquery){

returngetHibernateTemplate().find(query);

}

publicListfind(finalStringquery,finalObjectparameter){

returngetHibernateTemplate().find(query,parameter);

}

//以下是分页查询

publicPaginationSupportfindPageByCriteria(finalDetachedCriteriadetachedCriteria){

//通过detachedCriteri,一页显示记录数,当前是第几页查询(0)

returnfindPageByCriteria(detachedCriteria,PaginationSupport.PAGESIZE,0);

}

publicPaginationSupportfindPageByCriteria(finalDetachedCriteriadetachedCriteria,finalintstartIndex){

//用户指定当前为第几页

returnfindPageByCriteria(detachedCriteria,PaginationSupport.PAGESIZE,startIndex);

}

publicPaginationSupportfindPageByCriteria(finalDetachedCriteriadetachedCriteria,finalintpageSize,

finalintstartIndex){

//用户同时指定每页显示的记录数,和当前页为第几页

return(PaginationSupport)getHibernateTemplate().execute(newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession)throwsHibernateException{

Criteriacriteria=detachedCriteria.getExecutableCriteria(session);

inttotalCount=((Integer)criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();

criteria.setProjection(null);

Listitems=criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();

PaginationSupportps=newPaginationSupport(items,totalCount,pageSize,startIndex);

returnps;

}

},true);

}

publicListfindAllByCriteria(finalDetachedCriteriadetachedCriteria){

return(List)getHibernateTemplate().execute(newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession)throwsHibernateException{

Criteriacriteria=detachedCriteria.getExecutableCriteria(session);

returncriteria.list();

}

},true);

}

publicintgetCountByCriteria(finalDetachedCriteriadetachedCriteria){

Integercount=(Integer)getHibernateTemplate().execute(newHibernateCallback(){

publicObjectdoInHibernate(Sessionsession)throwsHibernateException{

Criteriacriteria=detachedCriteria.getExecutableCriteria(session);

returncriteria.setProjection(Projections.rowCount()).uniqueResult();

}

},true);

returncount.intValue();

}

}

用户在web层构造查询条件detachedCriteria,和可选的startIndex,调用业务bean的相应findByCriteria方法,返回一个PaginationSupport的实例ps。

ps.getItems()得到已分页好的结果集

ps.getIndexes()得到分页索引的数组

ps.getTotalCount()得到总结果数

ps.getStartIndex()当前分页索引

ps.getNextIndex()下一页索引

ps.getPreviousIndex()上一页索引

相关推荐