如何 用hibernate 实现分页
如何 用hibernate 实现分页,好像有Query.setFirstResult()/Query.setMaxResults() 但我不知道怎么使用
如何 用hibernate 实现分页,好像有Query.setFirstResult()/Query.setMaxResults() 但我不知道怎么使用
写了个类
import java.util.*;
import org.apache.log4j.*;
import net.sf.hibernate.*;
import net.sf.hibernate.type.*;
public class Paginator {
private static Logger _logger = Logger.getLogger(Paginator.class);
//--------------------------------------------------------------------------
private int pageNo; //page number
private int maxResults; //results per page
//--------------------------------------------------------------------------
private int resultCount; //result count
private int pageCount; //page count
private int realPageNo; //real page number(if you exceed the max page number,you will be at the last page)
//--------------------------------------------------------------------------
private String queryString; //query string for this query
private String countQueryString; //query string for this query to get the result count
//--------------------------------------------------------------------------
private Paginator() {
this(10, 1);
}
/**
* construct a Paginator object
* @param maxResults results per page
* @param pageNo page number
*/
public Paginator(int maxResults, int pageNo) {
this.setPageInfo(maxResults, pageNo);
}
public String toString() {
return
"\n------------------------------------------------------------\n" +
"Paginator:\n" +
"queryString: [" + this.queryString.trim() + "]\n" +
"countQueryString: [" + this.countQueryString + "]\n" +
"pageNo: [" + this.pageNo + "]\n" +
"maxResult: [" + this.maxResults + "]\n" +
"resultCount: [" + this.resultCount + "]\n" +
"pageCount: [" + this.pageCount + "]\n" +
"realPageNo: [" + this.realPageNo +
"]\n------------------------------------------------------------";
}
//--------------------------------------------------------------------------
private static final List _emptyList = Collections.unmodifiableList(new
ArrayList(0));
public List list(Session session, String queryString) throws
HibernateException {
return this.list(session, queryString, null);
}
public List list(Session session, String queryString, final String[] names,
final Object[] values,
final Type[] types) throws HibernateException {
final int max = names.length;
if (max != values.length || max != values.length)
throw new IllegalArgumentException("illegal argument");
return this.list(session, queryString, new QueryParasSetter() {
public void setQueryParameters(Query q) {
for (int i = 0; i < max; i++) {
q.setParameter(names, values, types);
}
}
});
}
/**
* perform pagination
* @param session session to perform database operation
* @param query your Query object with its parameters unset
* @param qps QueryParasSetter object to set the parameters of your Query object
* @return the result List
* @throws HibernateException
*/
public List list(Session session, String queryString, QueryParasSetter qps) throws
HibernateException {
this.queryString = queryString;
Query query = session.createQuery(queryString);
//-----------------------------------
List l = null;
Transaction tx = null;
try {
tx = session.beginTransaction();
Query countQuery = this.createCountQuery(session, queryString);
if (qps != null)
qps.setQueryParameters(countQuery);
this.resultCount = ( (Integer) (countQuery.iterate().next())).
intValue();
if (this.resultCount == 0) {
this.pageCount = 1;
this.realPageNo = 0;
l = this._emptyList;
}
else {
query.setMaxResults(this.maxResults);
//--------------------------------------------------------------
this.pageCount = this.resultCount / this.maxResults;
if (this.resultCount % this.maxResults != 0)
this.pageCount++;
this.realPageNo = this.pageNo;
if (this.realPageNo > this.pageCount)
this.realPageNo = this.pageCount;
int firstResult = this.maxResults * (this.realPageNo - 1);
query.setFirstResult(firstResult);
//--------------------------------------------------------------
if (qps != null)
qps.setQueryParameters(query);
l = query.list();
}
}
finally {
if (tx != null)
tx.commit();
}
return l;
}
//--------------------------------------------------------------------------
public void setPageInfo(int maxResults, int pageNo) {
if (maxResults <= 0)
throw new IllegalArgumentException("maxResults=" + maxResults);
if (pageNo <= 0)
throw new IllegalArgumentException("pageNo=" + pageNo);
this.pageNo = pageNo;
this.maxResults = maxResults;
}
//--------------------------------------------------------------------------
public int getResultCount() {
return resultCount;
}
public int getPageCount() {
return pageCount;
}
public int getRealPageNo() {
return realPageNo;
}
//--------------------------------------------------------------------------
/**
* create a Query object to get the result count return by Query q
* @param q your Query object
* @return
* @throws HibernateException
*/
/*private Query createCountQuery(Session session,Query q) throws HibernateException {
return createCountQuery(q.getQueryString());
}*/
private Query createCountQuery(Session session, String queryString) throws
HibernateException {
this._logger.debug("queryString:[" + queryString + "]");
String oqs = queryString.trim();
String qs = oqs.toLowerCase();
boolean hasSelect = qs.startsWith("select ");
int selectEndIndex = 0;
if (hasSelect) {
selectEndIndex += "select ".length();
}
int fromIndex = qs.indexOf("from ", selectEndIndex);
if (fromIndex < 0)
throw new RuntimeException("queryString error!");
String s = oqs.substring(selectEndIndex, fromIndex).trim();
if (s.length() == 0)
s = "*";
String fromStr = oqs.substring(fromIndex);
countQueryString = "select count(" + s + ") " + fromStr;
this._logger.debug("countQueryString:[" + countQueryString + "]");
Query countQuery = session.createQuery(countQueryString);
return countQuery;
}
}
import net.sf.hibernate.*;
public interface QueryParasSetter {
public void setQueryParameters(Query q);
}