基於hibernate實現的分頁技術

先說明一下基於hibernate實現分頁的原理,假如從數據庫取出100條數據,我們要讓每頁顯示10條,假如從30開始,只需要設置起始位置和最大的返回結果即可

先上代碼:注意傳進來的參數有 Page這類,後面有介紹

[javascript] view plain copy
  1. public List<Article> queryByPage(final String username, final Page page) {  
  2.         return this.getHibernateTemplate().executeFind(new HibernateCallback() {  
  3.             public Object doInHibernate(Session session)  
  4.                     throws HibernateException, SQLException {  
  5.                 Query query = session.createQuery("select art from Article art where art.username = ?");  
  6.                 //設置參數  
  7.                 query.setParameter(0, username);  
  8.                 //設置每頁顯示多少個,設置多大結果。  
  9.                 query.setMaxResults(page.getEveryPage());  
  10.                 //設置起點  
  11.                 query.setFirstResult(page.getBeginIndex());  
  12.                 return query.list();  
  13.             }  
  14.         });  

上面關鍵代碼是 setMaxResults(),和setFirstResult(),即設置最大顯示值和起點

這裏我們需要一個Page工具類,用來操作分頁。

Page.Java

[java] view plain copy
  1. package com.fenye;  
  2.   
  3. public class Page {  
  4.     // 1.每頁顯示數量(everyPage)  
  5.     private int everyPage;  
  6.     // 2.總記錄數(totalCount)  
  7.     private int totalCount;  
  8.     // 3.總頁數(totalPage)  
  9.     private int totalPage;  
  10.     // 4.當前頁(currentPage)  
  11.     private int currentPage;  
  12.     // 5.起始點(beginIndex)  
  13.     private int beginIndex;  
  14.     // 6.是否有上一頁(hasPrePage)  
  15.     private boolean hasPrePage;  
  16.     // 7.是否有下一頁(hasNextPage)  
  17.     private boolean hasNextPage;  
  18.   
  19.     public Page(int everyPage, int totalCount, int totalPage, int currentPage,  
  20.             int beginIndex, boolean hasPrePage, boolean hasNextPage) {  
  21.         this.everyPage = everyPage;  
  22.         this.totalCount = totalCount;  
  23.         this.totalPage = totalPage;  
  24.         this.currentPage = currentPage;  
  25.         this.beginIndex = beginIndex;  
  26.         this.hasPrePage = hasPrePage;  
  27.         this.hasNextPage = hasNextPage;  
  28.     }  
  29.   
  30.     //構造函數,默認  
  31.     public Page(){}  
  32.       
  33.     //構造方法,對所有屬性進行設置  
  34.       
  35.       
  36.     public int getEveryPage() {  
  37.         return everyPage;  
  38.     }  
  39.   
  40.     public void setEveryPage(int everyPage) {  
  41.         this.everyPage = everyPage;  
  42.     }  
  43.   
  44.     public int getTotalCount() {  
  45.         return totalCount;  
  46.     }  
  47.   
  48.     public void setTotalCount(int totalCount) {  
  49.         this.totalCount = totalCount;  
  50.     }  
  51.   
  52.     public int getTotalPage() {  
  53.         return totalPage;  
  54.     }  
  55.   
  56.     public void setTotalPage(int totalPage) {  
  57.         this.totalPage = totalPage;  
  58.     }  
  59.   
  60.     public int getCurrentPage() {  
  61.         return currentPage;  
  62.     }  
  63.   
  64.     public void setCurrentPage(int currentPage) {  
  65.         this.currentPage = currentPage;  
  66.     }  
  67.   
  68.     public int getBeginIndex() {  
  69.         return beginIndex;  
  70.     }  
  71.   
  72.     public void setBeginIndex(int beginIndex) {  
  73.         this.beginIndex = beginIndex;  
  74.     }  
  75.   
  76.     public boolean isHasPrePage() {  
  77.         return hasPrePage;  
  78.     }  
  79.   
  80.     public void setHasPrePage(boolean hasPrePage) {  
  81.         this.hasPrePage = hasPrePage;  
  82.     }  
  83.   
  84.     public boolean isHasNextPage() {  
  85.         return hasNextPage;  
  86.     }  
  87.   
  88.     public void setHasNextPage(boolean hasNextPage) {  
  89.         this.hasNextPage = hasNextPage;  
  90.     }  
  91.   
  92. }  
Page工具類主要是封裝頁面信息,一共多少數據啊,一頁顯示多少啊,起點的序號,總頁數,是否有上一頁下一頁,當前頁。

還需要一個操作page的工具類,PageUtil.java

[javascript] view plain copy
  1. package com.sanqing.fenye;  
  2. /* 
  3.  * 分頁信息輔助類 
  4.  */  
  5. public class PageUtil {  
  6.       
  7.     public static Page createPage(int everyPage,int totalCount,int currentPage) {  
  8.         everyPage = getEveryPage(everyPage);  
  9.         currentPage = getCurrentPage(currentPage);  
  10.         int totalPage = getTotalPage(everyPage, totalCount);  
  11.         int beginIndex = getBeginIndex(everyPage, currentPage);  
  12.         boolean hasPrePage = getHasPrePage(currentPage);  
  13.         boolean hasNextPage = getHasNextPage(totalPage, currentPage);  
  14.         return new Page(everyPage, totalCount, totalPage, currentPage,  
  15.                 beginIndex, hasPrePage,  hasNextPage);  
  16.     }  
  17.       
  18.     public static Page createPage(Page page,int totalCount) {  
  19.         int everyPage = getEveryPage(page.getEveryPage());  
  20.         int currentPage = getCurrentPage(page.getCurrentPage());  
  21.         int totalPage = getTotalPage(everyPage, totalCount);  
  22.         int beginIndex = getBeginIndex(everyPage, currentPage);  
  23.         boolean hasPrePage = getHasPrePage(currentPage);  
  24.         boolean hasNextPage = getHasNextPage(totalPage, currentPage);  
  25.         return new Page(everyPage, totalCount, totalPage, currentPage,  
  26.                 beginIndex, hasPrePage,  hasNextPage);  
  27.     }  
  28.       
  29.     //設置每頁顯示記錄數  
  30.     public static int getEveryPage(int everyPage) {  
  31.         return everyPage == 0 ? 10 : everyPage;  
  32.     }  
  33.       
  34.     //設置當前頁  
  35.     public static int getCurrentPage(int currentPage) {  
  36.         return currentPage == 0 ? 1 : currentPage;  
  37.     }  
  38.       
  39.     //設置總頁數,需要總記錄數,每頁顯示多少  
  40.     public static int getTotalPage(int everyPage,int totalCount) {  
  41.         int totalPage = 0;  
  42.         if(totalCount % everyPage == 0) {  
  43.             totalPage = totalCount / everyPage;  
  44.         } else {  
  45.             totalPage = totalCount / everyPage + 1;  
  46.         }  
  47.         return totalPage;  
  48.     }  
  49.       
  50.     //設置起始點,需要每頁顯示多少,當前頁  
  51.     public static int getBeginIndex(int everyPage,int currentPage) {  
  52.         return (currentPage - 1) * everyPage;  
  53.     }  
  54.       
  55.     //設置是否有上一頁,需要當前頁  
  56.     public static boolean getHasPrePage(int currentPage) {  
  57.         return currentPage == 1 ? false : true;  
  58.     }  
  59.       
  60.     //設置是否有下一個,需要總頁數和當前頁  
  61.     public static boolean getHasNextPage(int totalPage, int currentPage) {  
  62.         return currentPage == totalPage || totalPage == 0 ? false : true;  
  63.     }  
  64.       
  65. }  
創建Page只需要3個參數,每頁顯示多少數據,當前頁,總共多少數據,其他的4個參數都可以通過這三個計算出來

所以後面要創建Page,只需要調用這工具方法PageUtil.createPage(3個參數),就返回一Page.

返回的Page就是前面參數的Page,即要顯示的分頁

這樣就算完成了分頁的功能。

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