基于hibernate实现的分页技术

来源:互联网 发布:被子布料差异知乎 编辑:程序博客网 时间:2024/04/29 23:49

先说明一下基于hibernate实现分页的原理,假如从数据库取出100条数据,我们要让每页显示10条,假如从30开始,只需要设置起始位置和最大的返回结果即可

先上代码:注意传进来的参数有 Page这类,后面有介绍

[javascript] view plaincopy
  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 plaincopy
  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 plaincopy
  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,即要显示的分页

这样就算完成了分页的功能。

原创粉丝点击