`
aflyer
  • 浏览: 36134 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

关于Hibernate分页

阅读更多
1. page.java
java 代码
  1. package com.sclh.rsp.registercenter.common;   
  2.   
  3. public class Page {   
  4.     /** imply if the page has previous page */  
  5.     private boolean hasPrePage;   
  6.   
  7.     /** imply if the page has next page */  
  8.     private boolean hasNextPage;   
  9.   
  10.     /** the number of every page */  
  11.     private int everyPage;   
  12.   
  13.     /** the total page number */  
  14.     private int totalPage;   
  15.   
  16.     /** the number of current page */  
  17.     private int currentPage;   
  18.   
  19.     /** the begin index of the records by the current query */  
  20.     private int beginIndex;   
  21.        
  22.     private int totalCount;   
  23.   
  24.     /** The default constructor */  
  25.     public Page() {   
  26.   
  27.     }   
  28.   
  29.     /** construct the page by everyPage     
  30.      * @param everyPage    
  31.      * */  
  32.     public Page(int everyPage) {   
  33.         this.everyPage = everyPage;   
  34.     }   
  35.   
  36.     /** The whole constructor */  
  37.     public Page(boolean hasPrePage, boolean hasNextPage, int everyPage,   
  38.             int totalPage, int currentPage, int beginIndex,int totalCount) {   
  39.         this.hasPrePage = hasPrePage;   
  40.         this.hasNextPage = hasNextPage;   
  41.         this.everyPage = everyPage;   
  42.         this.totalPage = totalPage;   
  43.         this.currentPage = currentPage;   
  44.         this.beginIndex = beginIndex;   
  45.         this.totalCount = totalCount;   
  46.     }   
  47.   
  48.     /**    
  49.      * @return     
  50.      * Returns the beginIndex.    
  51.      */  
  52.     public int getBeginIndex() {   
  53.         return beginIndex;   
  54.     }   
  55.   
  56.     /**    
  57.      * @param beginIndex     
  58.      * The beginIndex to set.    
  59.      */  
  60.     public void setBeginIndex(int beginIndex) {   
  61.         this.beginIndex = beginIndex;   
  62.     }   
  63.   
  64.     /**    
  65.      * @return     
  66.      * Returns the currentPage.    
  67.      */  
  68.     public int getCurrentPage() {   
  69.         return currentPage;   
  70.     }   
  71.   
  72.     /**    
  73.      * @param currentPage     
  74.      * The currentPage to set.    
  75.      */  
  76.     public void setCurrentPage(int currentPage) {   
  77.         this.currentPage = currentPage;   
  78.     }   
  79.   
  80.     /**    
  81.      * @return     
  82.      * Returns the everyPage.    
  83.      */  
  84.     public int getEveryPage() {   
  85.         return everyPage;   
  86.     }   
  87.   
  88.     /**    
  89.      * @param everyPage     
  90.      * The everyPage to set.    
  91.      */  
  92.     public void setEveryPage(int everyPage) {   
  93.         this.everyPage = everyPage;   
  94.     }   
  95.   
  96.     /**    
  97.      * @return     
  98.      * Returns the hasNextPage.    
  99.      */  
  100.     public boolean getHasNextPage() {   
  101.         return hasNextPage;   
  102.     }   
  103.   
  104.     /**    
  105.      * @param hasNextPage     
  106.      * The hasNextPage to set.    
  107.      */  
  108.     public void setHasNextPage(boolean hasNextPage) {   
  109.         this.hasNextPage = hasNextPage;   
  110.     }   
  111.   
  112.     /**    
  113.      * @return     
  114.      * Returns the hasPrePage.    
  115.      */  
  116.     public boolean getHasPrePage() {   
  117.         return hasPrePage;   
  118.     }   
  119.   
  120.     /**    
  121.      * @param hasPrePage     
  122.      * The hasPrePage to set.    
  123.      */  
  124.     public void setHasPrePage(boolean hasPrePage) {   
  125.         this.hasPrePage = hasPrePage;   
  126.     }   
  127.   
  128.     /**    
  129.      * @return Returns the totalPage.    
  130.      *     
  131.      */  
  132.     public int getTotalPage() {   
  133.         return totalPage;   
  134.     }   
  135.   
  136.     /**    
  137.      * @param totalPage     
  138.      * The totalPage to set.    
  139.      */  
  140.     public void setTotalPage(int totalPage) {   
  141.         this.totalPage = totalPage;   
  142.     }   
  143.   
  144.     public int getTotalCount() {   
  145.         return totalCount;   
  146.     }   
  147.   
  148.     public void setTotalCount(int totalCount) {   
  149.         this.totalCount = totalCount;   
  150.     }   
  151. }   

 

2. PageUtil.java

java 代码
  1. package com.sclh.rsp.registercenter.common;   
  2.   
  3. public class PageUtil {   
  4.   
  5.     /**  
  6.      * Use the origin page to create a new page  
  7.      *   
  8.      * @param page  
  9.      * @param totalRecords  
  10.      * @return  
  11.      */  
  12.     public static Page createPage(Page page, int totalRecords) {   
  13.         return createPage(page.getEveryPage(), page.getCurrentPage(),   
  14.                 totalRecords);   
  15.     }   
  16.   
  17.     /**  
  18.      * the basic page utils not including exception handler  
  19.      *   
  20.      * @param everyPage  
  21.      * @param currentPage  
  22.      * @param totalRecords  
  23.      * @return page  
  24.      */  
  25.     public static Page createPage(int everyPage, int currentPage,   
  26.             int totalRecords) {   
  27.         everyPage = getEveryPage(everyPage);   
  28.         currentPage = getCurrentPage(currentPage);   
  29.         int beginIndex = getBeginIndex(everyPage, currentPage);   
  30.         int totalPage = getTotalPage(everyPage, totalRecords);   
  31.         boolean hasNextPage = hasNextPage(currentPage, totalPage);   
  32.         boolean hasPrePage = hasPrePage(currentPage);   
  33.   
  34.         return new Page(hasPrePage, hasNextPage, everyPage, totalPage,   
  35.                 currentPage, beginIndex, totalRecords);   
  36.     }   
  37.   
  38.     private static int getEveryPage(int everyPage) {   
  39.         return everyPage == 0 ? 10 : everyPage;   
  40.     }   
  41.   
  42.     private static int getCurrentPage(int currentPage) {   
  43.         return currentPage == 0 ? 1 : currentPage;   
  44.     }   
  45.   
  46.     private static int getBeginIndex(int everyPage, int currentPage) {   
  47.         return (currentPage - 1) * everyPage;   
  48.     }   
  49.   
  50.     private static int getTotalPage(int everyPage, int totalRecords) {   
  51.         int totalPage = 0;   
  52.   
  53.         if (totalRecords % everyPage == 0)   
  54.             totalPage = totalRecords / everyPage;   
  55.         else  
  56.             totalPage = totalRecords / everyPage + 1;   
  57.   
  58.         return totalPage;   
  59.     }   
  60.   
  61.     private static boolean hasPrePage(int currentPage) {   
  62.         return currentPage == 1 ? false : true;   
  63.     }   
  64.   
  65.     private static boolean hasNextPage(int currentPage, int totalPage) {   
  66.         return currentPage == totalPage || totalPage == 0 ? false : true;   
  67.     }   
  68.   
  69. }   

 

3. 应用

java 代码
  1. ......   
  2.     /**  
  3.      * @throws Exception   
  4.      * @see com.sclh.rsp.servicecenter.service.IBaseService#queryList(java.lang.String, int)  
  5.      */  
  6.     public Result queryList(String hql, int currentPage, int everyPage){   
  7.         List list = null;   
  8.         Result result = null;   
  9.   
  10.         Page page = new Page();   
  11.         page.setEveryPage(everyPage);   
  12.         page.setCurrentPage(currentPage);   
  13.   
  14.         int totalRecords = 0;   
  15.   
  16.         try {   
  17.             totalRecords = adjunctDataDAO.getCount(hql);   
  18.             if (totalRecords != 0) {   
  19.                 page = PageUtil.createPage(page, totalRecords);   
  20.                 list = adjunctDataDAO.getByPage(hql, page);   
  21.                 result = new Result(page, list);   
  22.             } else {   
  23.                 // throw new ObjectNotFoundException("adjunctDataNotExist",   
  24.                 // null);   
  25.                    
  26.             }   
  27.         } catch (Exception e) {   
  28.             log.error(this.getClass()   
  29.                     + "中 queryList(String hql, int currentPage) 操作失败!"  
  30.                     + e.getMessage());   
  31.         }   
  32.         return result;   
  33.     }   
  34. ................  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics