Hibernate範型-離線分頁實現(上)

         我們現在學習的是基於Hibernate的離線查詢分頁,我們在Web設計中可以選用Struts框架、JSTL等等來實現其功能。<o:p></o:p>

首先,我們要理解分頁的算法,也要講究效率,也就是說我們不能把所有的記錄取出來,我們只取一個頁面中需要顯示的紀錄,那麼怎麼去實現是我們關心的問題!不要着急,重在理解而是單純技術實現。我採用Web框架兩種方式來實現,attention!<o:p></o:p>

分頁算法,也就是我們提供給客戶方便的操作。一般有哪些東西呢?<o:p></o:p>

       上頁、下頁、首頁、末頁、導航到X頁,頁碼導航。說到這裏我們先放下,我們來說說取出頁面的記錄,也就是隻取一段的記錄。打開Hibernate API 看到Criteria 接口,public Criteria setMaxResults(int maxResults),意思爲:設置記錄對象數目的上限,第二方法,public Criteria setFirstResult setFirstResult(int firstResult),意思:設置開始取得記錄對象的下標,其中的第一對象下標爲0。這樣我們可以看出Hibernate提供很好的方法給我們實現分頁。<o:p></o:p>

       我們產生了思路:我們只有設置firstResult得下標,我們這裏把它叫做遊標。我們只要控制遊標就可以控制取得的記錄就是我們想要得到的,下面要做的工作就是我們來控制index,下面我寫了一個HPage的類來實現這個工作,也就是取得一些相關的值,其中我們關係的有:當前的頁碼、當前的遊標等等,下面我們結合代碼來說明下!~~~<o:p></o:p>

實現代碼如下:

java 代碼
  1. public class HPage {   
  2.     private int countPage;  //頁面的數量   
  3.     private int startIndex=0//紀錄遊標       
  4.     private int recordPerPage;  //每頁中的記錄數量   
  5.     private int pageIndex=1;  //初始的頁碼   
  6.     private boolean nextPage=true;   
  7.     private boolean previousPage=false;   
  8.     private boolean firstPage=true;   
  9.     private boolean lastPage=false;   
  10.     public int getCountPage()  //取得頁面的數量   
  11.     {   
  12.         return countPage;   
  13.     }   
  14.     public int getStartIndex() //取得遊標的位置   
  15.     {   
  16.         return startIndex;   
  17.     }   
  18.     public HPage(int countRecord,int recordPerPage)     
  19.     //coutRecord參數:紀錄的總數量;recordPerPage參數:設置每頁的記錄數量   
  20.     {   
  21.         this.recordPerPage=recordPerPage;   
  22.         if(countRecord % recordPerPage == 0)   
  23.             countPage=countRecord / recordPerPage;   
  24.         else  
  25.             countPage=countRecord / recordPerPage +1;   
  26.     }   
  27.     public int getPageIndex() // 取得當前頁碼   
  28.     {   
  29.         return pageIndex;   
  30.     }   
  31.     public int nextPage() // 遊標下一頁滾動   
  32.     {   
  33.         pageIndex++;   
  34.         if(isOnlyOne())   
  35.         {   
  36.             nextPage=false;   
  37.             previousPage=false;   
  38.             firstPage=false;   
  39.             lastPage=false;   
  40.         }   
  41.         else  
  42.         {   
  43.             previousPage=true;   
  44.             firstPage=true;   
  45.         }   
  46.         if(pageIndex > countPage)   
  47.             return lastPage();   
  48.         else  
  49.         {   
  50.             return (startIndex=forwardPage(pageIndex));   
  51.         }   
  52.     }   
  53.     public int previousPage()  // 遊標上一頁滾動   
  54.     {   
  55.         pageIndex--;   
  56.         if(isOnlyOne())   
  57.         {   
  58.             nextPage=false;   
  59.             previousPage=false;   
  60.             firstPage=false;   
  61.             lastPage=false;   
  62.         }   
  63.         else  
  64.         {   
  65.             nextPage=true;   
  66.             lastPage=true;   
  67.         }   
  68.         if(pageIndex <= 1)   
  69.             return firstPage();   
  70.         else  
  71.         {   
  72.             return (startIndex=forwardPage(pageIndex));   
  73.         }   
  74.     }   
  75.     public int lastPage() // 遊標向末頁滾動   
  76.     {   
  77.         pageIndex=countPage;   
  78.         if(isOnlyOne())   
  79.         {   
  80.             nextPage=false;   
  81.             previousPage=false;   
  82.             firstPage=false;   
  83.             lastPage=false;   
  84.         }   
  85.         else  
  86.         {   
  87.             nextPage=false;   
  88.             previousPage=true;   
  89.             firstPage=true;   
  90.             lastPage=false;   
  91.         }   
  92.         return (startIndex=(countPage-1) * recordPerPage);   
  93.     }   
  94.     public int firstPage() // 遊標向首頁滾動   
  95.     {   
  96.         pageIndex=1;   
  97.         if(isOnlyOne())   
  98.         {   
  99.             nextPage=false;   
  100.             previousPage=false;   
  101.             firstPage=false;   
  102.             lastPage=false;   
  103.         }   
  104.         else  
  105.         {   
  106.             nextPage=true;   
  107.             previousPage=false;   
  108.             firstPage=false;   
  109.             lastPage=true;   
  110.         }   
  111.         return (startIndex=0);   
  112.     }   
  113.     public int forwardPage(int pageIndex) //導向制定頁面   
  114.     {   
  115.            
  116.         if(pageIndex >= countPage)   
  117.         {   
  118.             pageIndex=countPage;   
  119.             return (startIndex=lastPage());   
  120.         }   
  121.         else if(pageIndex <= 1)   
  122.         {   
  123.             pageIndex=1;   
  124.             return startIndex=firstPage();   
  125.         }   
  126.         this.pageIndex=pageIndex;   
  127.         if(isOnlyOne())   
  128.         {   
  129.             nextPage=false;   
  130.             previousPage=false;   
  131.             firstPage=false;   
  132.             lastPage=false;   
  133.         }   
  134.         else  
  135.         {   
  136.             nextPage=true;   
  137.             previousPage=true;   
  138.             firstPage=true;   
  139.             lastPage=true;   
  140.         }   
  141.         return (startIndex=(this.pageIndex -1)* recordPerPage);        
  142.     }   
  143.     private boolean isOnlyOne() //是否只有一頁   
  144.     {   
  145.         return countPage==1 ? true : false;   
  146.     }   
  147.     public boolean isNextPage() //是否有下頁   
  148.     {   
  149.         return nextPage;   
  150.     }   
  151.     public boolean isPreviousPage() //是否有上頁   
  152.     {   
  153.         return previousPage;   
  154.     }   
  155.     public boolean isFirstPage() //是否首頁   
  156.     {   
  157.         return firstPage;   
  158.     }   
  159.     public boolean isLastPage() //是否末頁   
  160.     {   
  161.         return lastPage;   
  162.     }      
  163. }   

 

不是很難吧!好的,現在我們做好的工作就是取得了我們要的數值,下面要做的是加到Hibernate查詢裏面去,下面的工作很簡單。但是我們要注意幾個問題,第一個問題,我們的設計的目的不是爲了解決一個問題,而是抽象出一個方案提供給相似的問題的解決方案。這樣的話,我們最大化重利用代碼,那麼我們採用範性的特點(注意JDK5.0的特徵,以前的版本會報錯誤)。下面我分析一下代碼:<o:p></o:p>

java 代碼
  1. public class DisplayPageRecordList<t></t> { //泛型實現   
  2.     private int startIndex;   
  3.     private static final int DEFAULT_RECORD_PER_PAGE=5//默認:每頁中5條記錄   
  4.     private List<t></t> list;      
  5.     private HPage page;   
  6.     private int recordPerPage=DEFAULT_RECORD_PER_PAGE;   
  7.     private final Session session=HibernateSessionFactory.getSession();   
  8.     private DetachedCriteria deCriteria;   
  9.     private Criteria criteria;     
  10.     private int countRecord;   
  11.     public DisplayPageRecordList(String className) throws ClassNotFoundException   
  12.     //className參數:類的全定名稱   
  13.     {          
  14.         deCriteria=DetachedCriteria.forClass(Class.forName(className)); //創造離線的查詢   
  15.         criteria=deCriteria.getExecutableCriteria(session);   
  16.         criteria.setCacheable(true);           
  17.         doInit();   
  18.         page=new HPage(countRecord,recordPerPage);             
  19.     }   
  20.     public DisplayPageRecordList(int recordPerPage,String className) throws ClassNotFoundException   
  21.     //recordPerPage參數:每頁的紀錄的條目數,className參數:同上個構造方法中   
  22.     {   
  23.            
  24.         deCriteria=DetachedCriteria.forClass(Class.forName(className));   
  25.         criteria=deCriteria.getExecutableCriteria(session);   
  26.         criteria.setCacheable(true);   
  27.         this.recordPerPage=recordPerPage;   
  28.         doInit();   
  29.         page=new HPage(countRecord,recordPerPage);             
  30.     }      
  31.     public DisplayPageRecordList(int recordPerPage,String className,Criterion criterion) throws ClassNotFoundException   
  32.     //recordPerPage參數:同上個構造方法中,className參數:同上個構造方法中,criterion參數:設置查詢條件   
  33.     {   
  34.            
  35.         deCriteria=DetachedCriteria.forClass(Class.forName(className));   
  36.         criteria=deCriteria.getExecutableCriteria(session);   
  37.         criteria.add(criterion);   
  38.         criteria.setCacheable(true);           
  39.         doInit();   
  40.         page=new HPage(countRecord,recordPerPage);             
  41.     }      
  42.     public void setRecordPerPage(int recordPerPage)   
  43.     {   
  44.         this.recordPerPage=recordPerPage;   
  45.         page=new HPage(countRecord,recordPerPage);     
  46.     }   
  47.     private void doInit()   
  48.     {   
  49.         this.countRecord=criteria.list().size(); //計算數據庫中所有的記錄數        
  50.     }   
  51.     public void addCriterion(Criterion criterion)   
  52.     {   
  53.         criteria=criteria.add(criterion); //更改所有所有查詢條件   
  54.         doInit();   
  55.     }   
  56.     public void setCacheable(boolean cache) //是否啓用緩存   
  57.     {   
  58.         criteria.setCacheable(cache);   
  59.     }   
  60.     public List doNextPage() //查詢出下頁中的紀錄集   
  61.     {   
  62.         startIndex=page.nextPage();   
  63.         criteria.setFirstResult(startIndex);   
  64.         criteria.setMaxResults(this.recordPerPage);   
  65.         list=criteria.list();   
  66.         return list;   
  67.     }   
  68.     public List doPreviousPage() //查詢出上頁中的紀錄集   
  69.     {   
  70.         startIndex=page.previousPage();   
  71.         criteria.setFirstResult(startIndex);   
  72.         criteria.setMaxResults(this.recordPerPage);   
  73.         list=criteria.list();   
  74.         return list;   
  75.     }   
  76.     public List doFirstPage() //查詢出首頁中的紀錄集   
  77.     {   
  78.         startIndex=page.firstPage();   
  79.         criteria.setFirstResult(startIndex);   
  80.         criteria.setMaxResults(this.recordPerPage);   
  81.         list=criteria.list();   
  82.         return list;   
  83.     }   
  84.     public List doLastPage() //查詢出末頁中的紀錄集   
  85.     {   
  86.         startIndex=page.lastPage();   
  87.         criteria.setFirstResult(startIndex);   
  88.         criteria.setMaxResults(this.recordPerPage);   
  89.         list=criteria.list();   
  90.         return list;   
  91.     }   
  92.     public List doForward(int pageIndex)   
  93.     {   
  94.         startIndex=page.forwardPage(pageIndex);   
  95.         criteria.setFirstResult(startIndex);   
  96.         criteria.setMaxResults(this.recordPerPage);   
  97.         list=criteria.list();   
  98.         return list;   
  99.     }   
  100.     public HPage getPage() //取得頁面對象   
  101.     {   
  102.         return this.page;   
  103.     }      
  104. }   

 

以上代碼中,實現了泛型-離線查詢的頁面的代碼。上篇完,下篇我們來使用StrutsJSTL在網頁上測試<o:p></o:p>

 

<o:p></o:p>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章