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