使用 HibernateTemplate 实现分页查询 (HibernateCallback接口)
/**
*
*/
package springdao;
import hibernatedao.HibernateSessionFactory;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
/**
* 通用 DAO 包
* @author kiant
* @version Sep 7, 2008
*/
public class CommomsDAO {
private static final Log log = LogFactory.getLog(EcOpusDAO.class);
//获得会话
private static HibernateTemplate hibernateTemplate = new HibernateTemplate(HibernateSessionFactory.getSessionFactory());
/**
* 分页通用方法
* @param hql HQL查询语句
* @param offset 起始记录下标
* @param lengh 读取记录数
* @return List 结果集
*/
public static List getListForPage(final String hql, final int offset, final int lengh) {
log.debug("finding ListForPage");
try {
List list = hibernateTemplate.executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List list2 = session.createQuery(hql)
.setFirstResult(offset)
.setMaxResults(lengh)
.list();
return list2;
}});
return list;
} catch (RuntimeException re) {
log.error("find ListForPage failed", re);
throw re;
}
}
}
*
*/
package springdao;
import hibernatedao.HibernateSessionFactory;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
/**
* 通用 DAO 包
* @author kiant
* @version Sep 7, 2008
*/
public class CommomsDAO {
private static final Log log = LogFactory.getLog(EcOpusDAO.class);
//获得会话
private static HibernateTemplate hibernateTemplate = new HibernateTemplate(HibernateSessionFactory.getSessionFactory());
/**
* 分页通用方法
* @param hql HQL查询语句
* @param offset 起始记录下标
* @param lengh 读取记录数
* @return List 结果集
*/
public static List getListForPage(final String hql, final int offset, final int lengh) {
log.debug("finding ListForPage");
try {
List list = hibernateTemplate.executeFind(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List list2 = session.createQuery(hql)
.setFirstResult(offset)
.setMaxResults(lengh)
.list();
return list2;
}});
return list;
} catch (RuntimeException re) {
log.error("find ListForPage failed", re);
throw re;
}
}
}