使用Hibernate3实现分页功能

 

 http://blog.csdn.net/zhjb1025/archive/2006/04/19/668631.aspx

  整理的代码如下:

  Page.Java接口

packageorg.domain.scrm4u.helper;
importjava.util.List;
publicinterfacePage{
  booleanisFirstPage();
  booleanisLastPage();
  booleanhasNextPage();
  booleanhASPreviousPage();
  intgetLastPageNumber();
  List<?>getThisPageElements();
  intgetTotalNumberOfElements();
  intgetThisPageFirstElementNumber();
  intgetThisPageLastElementNumber();
  intgetNextPageNumber();
  intgetPreviousPageNumber();
  intgetPageSize();
  intgetPageNumber();
}
 

  ListPage.java实现:

packageorg.domain.scrm4u.helper.impl;
importjava.util.List;
importorg.domain.scrm4u.helper.Page;
publicclassListPageimplementsPage{
  privateList<?>elements;
  privateintpageSize;
  privateintpageNumber;
  publicListPage(List<?>elements,intpageNumber,intpageSize){
    this.elements=elements;
    this.pageSize=pageSize;
    this.pageNumber=pageNumber;
    if(Integer.MAX_VALUE==this.pageNumber)this.pageNumber=(getTotalNumberOfElements()/this.pageSize);
  }
  publicbooleanisFirstPage(){
    returngetPageNumber()==0;
  }
  publicbooleanisLastPage(){
    returngetPageNumber()>=getLastPageNumber();
  }
  publicbooleanhasNextPage(){
    return((getPageNumber()+1)*getPageSize())<(getTotalNumberOfElements()+1);
  }
  publicbooleanhasPreviousPage(){
    returngetPageNumber()>0;
  }
  publicintgetLastPageNumber(){
    doubletotalResults=newInteger(getTotalNumberOfElements()).doubleValue();
    returnnewDouble(Math.floor(totalResults/getPageSize())).intValue();
  }
  publicList<?>getThisPageElements(){
    finalintstart=getPageNumber()*getPageSize();
    returnelements.subList(
          Math.min(start,getTotalNumberOfElements()+1), 
          Math.min(start+getPageSize(),getTotalNumberOfElements()+1)
       );
  }
  publicintgetTotalNumberOfElements(){
    returnelements.size()-1;
  }
  publicintgetThisPageFirstElementNumber(){
    returngetPageNumber()*getPageSize()+1;
  }
  publicintgetThisPageLastElementNumber(){
    intfullPage=getThisPageFirstElementNumber()+getPageSize()-1;
    returngetTotalNumberOfElements()<fullPage?getTotalNumberOfElements():fullPage;
  }
  publicintgetNextPageNumber(){
    returngetPageNumber()+1;
  }
  publicintgetPreviousPageNumber(){
    returngetPageNumber()-1;
  }
  publicintgetPageSize(){
    returnpageSize;
  }
  publicintgetPageNumber(){
    returnpageNumber;
  }
}
 

 

 

 HibernatePage.Java实现:

packageorg.domain.scrm4u.helper.impl;
importjava.util.List;
importorg.domain.scrm4u.helper.Page;
importorg.hibernate.HibernateException;
importorg.hibernate.Query;
importorg.hibernate.ScrollMode;
importorg.hibernate.ScrollableResults;
publicclassHibernatePageimplementsPage{
  protectedList<?>elements;
  protectedintpageSize;
  protectedintpageNumber;
  protectedinttotalElements=0;
  privateScrollableResultsscrollableResults;
  privateHibernatePage(intpageNumber,intpageSize){
    this.pageNumber=pageNumber;
    this.pageSize=pageSize;
  }
  publicbooleanisFirstPage(){
    returngetPageNumber()==0;
  }
  publicbooleanisLastPage(){
    returngetPageNumber()>=getLastPageNumber();
  }
  publicbooleanhasNextPage(){
    returnelements.size()>getPageSize();
  }
  publicbooleanhASPreviousPage(){
    returngetPageNumber()>0;
  }
  publicintgetLastPageNumber(){
    doubletotalResults=newInteger(getTotalNumberOfElements()).doubleValue();
    returnnewDouble(Math.floor(totalResults/getPageSize())).intValue();
  }
  publicList<?>getThisPageElements(){
    returnhasNextPage()?elements.subList(0,getPageSize()):elements;
  }
  publicintgetTotalNumberOfElements(){
    returntotalElements;
  }
  publicintgetThisPageFirstElementNumber(){
    returngetPageNumber()*getPageSize()+1;
  }
  publicintgetThisPageLastElementNumber(){
    intfullPage=getThisPageFirstElementNumber()+getPageSize()-1;
    returngetTotalNumberOfElements()<fullPage?getTotalNumberOfElements():fullPage;
  }
  publicintgetNextPageNumber(){
    returngetPageNumber()+1;
  }
  publicintgetPreviousPageNumber(){
    returngetPageNumber()-1;
  }
  publicintgetPageSize(){
    returnpageSize;
  }
  publicintgetPageNumber(){
    returnpageNumber;
  }
  publicstaticHibernatePagegetScrollPage(Queryquery,intpageNumber,intpageSize,ScrollModescrollMode){
    HibernatePagesp=newHibernatePage(pageNumber,pageSize);
    try{
      sp.scrollableResults=query.scroll(scrollMode);
      if(scrollMode==ScrollMode.SCROLL_SENSITIVE){
        sp.scrollableResults.last();
        sp.totalElements=sp.scrollableResults.getRowNumber();
      }else{
        sp.totalElements=sp.calculateTotalElementsByList(query);
      }
      sp.determineElements(query);
    }catch(HibernateExceptione){
      e.printStackTrace();
    }
    returnsp;
  }
  privatevoiddetermineElements(Queryquery)throwsHibernateException{
    if(Integer.MAX_VALUE==this.pageNumber)this.pageNumber=(getTotalNumberOfElements()/this.pageSize);
    elements=query.setFirstResult(this.pageNumber*this.pageSize).setMaxResults(this.pageSize+1).list();
  }
  privateintcalculateTotalElementsByList(Queryquery)throwsHibernateException{
    returnquery.list().size();
  }
}
 

简单分析一下:

  ListPage实现需要把查询的结果集全部取出来。

  我们着重还是分析一下HibernatePage的实现。

  测试代码如下:

importJava.io.IOException;
importjava.sql.SQLException;
importjava.util.List;
importorg.hibernate.Query;
importorg.hibernate.ScrollMode;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.cfg.AnnotationConfiguration;
importorg.hibernate.cfg.Configuration;
importorg.domain.scrm4u.helper.Page;
importorg.domain.scrm4u.helper.impl.HibernatePage;
importorg.domain.scrm4u.entity.TPsn;
publicclassBusinessService{
  publicstaticvoidmain(String[]args)throwsIOException,SQLException{
    Configurationconfig= newAnnotationConfiguration().configure();
    SessionFactorysessionFactory=config.buildSessionFactory();   
    Sessionsession=sessionFactory.openSession();
    StringHql="fromTPsn";
    Queryquery=session.createQuery(Hql);
    Pagepage=HibernatePage.getScrollPage(query,0,10,ScrollMode.SCROLL_SENSITIVE);
    List<?>list=page.getThisPageElements();
    for(inti=0;i<list.size();i++){
      TPsnpsn=(TPsn)list.get(i);
      System.out.println(psn.getPsnNo()+"t"+psn.getPsnPnm()+"t"+psn.getPsnBrthDt());
    } 
    session.close();
    sessionFactory.close();
  }
}
 

 

  测试中发现:

  使用ScrollMode.SCROLL_SENSITIVE比ScrollMode.FORWARD_ONLY在性能上要快很多。具体选择要看使用的JDBC驱动是否支撑了,支持scroll的话就可以使用ScrollableResults类来处理结果集了。

  当然,我们还可以配合缓存技术让翻页更有效率。

  关于缓存请参看http://blog.csdn.net/kunshan_shenbin/archive/2008/09/03/2874992.ASPx。

你可能感兴趣的:(Hibernate,分页查询)