Hibernate分页

写了一个公共类:

View Code
  1 package Util;
  2 
  3 import java.util.*;
  4 
  5 import org.hibernate.Query;
  6 import org.hibernate.Session;
  7 import org.hibernate.SessionFactory;
  8 import org.hibernate.cfg.AnnotationConfiguration;
  9 
 10 /**
 11  * 这个是用于Hibernate分页的公共库,构造方法必须要有表名
 12  * @author chj
 13  *
 14  */
 15 public class PagingUtil {
 16 
 17     private static SessionFactory sf = null;
 18     private String tableName = "";
 19     private int pageSize = 5;        //每页显示数据量,默认为5
 20     private int pageno = 1;        //当前页数,默认首页
 21     private int pagecount = -1;    //总页数
 22     private int datacount = -1;    //数量总数
 23     
 24     public PagingUtil(String tableName){
 25         this.tableName = tableName;
 26         if(sf == null){
 27             sf = new AnnotationConfiguration().configure().buildSessionFactory();
 28         }
 29         calPagecount();
 30     }
 31     
 32     public PagingUtil(String tableName,int pageSize){
 33         this.tableName = tableName;
 34         this.pageSize = pageSize;
 35         if(sf == null){
 36             sf = new AnnotationConfiguration().configure().buildSessionFactory();
 37         }
 38         calPagecount();
 39     }
 40     
 41     private static Session getSession(){
 42         return sf.getCurrentSession();
 43     }
 44     
 45     /**
 46      * 算出数据总量和总页数
 47      */
 48     public void calPagecount(){
 49         Session s = this.getSession();
 50         s.beginTransaction();
 51         try{
 52             Query q = s.createQuery("from " + this.tableName);
 53             List l = (List)q.list();
 54             System.out.println(l.size());
 55             this.datacount = l.size();
 56             if(datacount%pageSize == 0){
 57                 this.pagecount = datacount/pageSize;
 58             }else{
 59                 this.pagecount = datacount/pageSize + 1;
 60             }
 61             s.getTransaction().commit();
 62         }catch(Exception e){
 63             s.getTransaction().rollback();
 64         }
 65     }
 66 
 67     /**
 68      * 获取某页的数据
 69      * @param pageno
 70      * @return
 71      */
 72     public List getPagingResults(int pageno){
 73         this.pageno = pageno;
 74         List l = new ArrayList();
 75         Session s = this.getSession();
 76         s.beginTransaction();
 77         try{
 78             Query q = s.createQuery("from " + this.tableName);
 79             q.setFirstResult((this.pageno-1)*this.pageSize).setMaxResults(this.pageSize);
 80             l = (List)q.list();
 81             /*if(l.size() <= 0){
 82                 l = null;
 83             }*/
 84             s.getTransaction().commit();
 85         }catch(Exception e){
 86             s.getTransaction().rollback();
 87             return l;
 88         }
 89         return l;
 90     }
 91     
 92     /**
 93      * 返回表名
 94      * @return
 95      */
 96     public String getTableName() {
 97         return tableName;
 98     }
 99 
100     /**
101      * 设置表名
102      * @param tableName
103      */
104     public void setTableName(String tableName) {
105         this.tableName = tableName;
106     }
107 
108     /**
109      * 返回每页显示数据数
110      * @return
111      */
112     public int getPageSize() {
113         return pageSize;
114     }
115 
116     /**
117      * 设置每页显示数据数
118      * @param pageSize
119      */
120     public void setPageSize(int pageSize) {
121         this.pageSize = pageSize;
122     }
123 
124     /**
125      * 返回当前页码
126      * @return
127      */
128     public int getPageno() {
129         return pageno;
130     }
131 
132     /**
133      * 设置当前页码
134      * @param pageno
135      */
136     public void setPageno(int pageno) {
137         this.pageno = pageno;
138     }
139 
140     /**
141      * 返回总页数
142      * @return
143      */
144     public int getPagecount() {
145         return pagecount;
146     }
147 
148     /**
149      * 返回数据总数
150      * @return
151      */
152     public int getDatacount() {
153         return datacount;
154     }
155 
156     /**
157      * 是否有上一页
158      * @return
159      */
160     public boolean hasPre(){
161         
162         return (pageno-1)>0;
163     }
164     
165     /**
166      * 是否有下一页
167      * @return
168      */
169     public boolean hasNext(){
170         
171         return (pageno+1)<=pagecount;
172     }
173 }

在构造方法中传入表名和每页显示数(默认为5),然后就可以通过getPagingResults(int pageno)方法来获得某一页的数据,返回一个list对象.

例如:

View Code
1 PagingUtil my = new PagingUtil("_group where uid="+uuid);
2     List<_group> gs = (List<_group>)my.getPagingResults(Integer.parseInt(pageno));

 如果是想用模糊搜索,也可以,看下面的例子:

View Code
1                 PagingUtil testp = new PagingUtil("file where filename like '%123%' or swfname like '%.swf%'", 3);
2         List<file> us = testp.getPagingResults(1);
3         for(int i=0;i<us.size();i++){
4             System.out.println(us.get(i).getFilename());
5         }    

 

posted on 2012-12-30 13:38  saobchj  阅读(334)  评论(1编辑  收藏  举报

导航