使用 HibernateTemplate 实现分页查询 (HibernateCallback接口)
2011-11-23 09:49
543 查看
/**
*
*/
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;
}
}
}
相关文章推荐
- 使用 HibernateTemplate 实现分页查询 (HibernateCallback接口)
- Hibernate里面如何使用DetachedCriteriaCriteria 实现多条件分页查询
- 用hibernate实现动态查询+分页的模板,另附struts2的标签在分页中的使用
- Hibernate使用原生的动态sql实现带条件的查询分页功能
- DaoImpl中实现查询分页-使用HibernateCallback来做更加方便
- 使用Hibernate criteria进行分页查询时,如何实现一次查询取得总记录数和分页后结果集
- 使用FileUpload工具实现文件上传与使用ThreadLocal容器传递数据实现分页查询下载
- 使用传入的总记录数实现一条sql语句完成分页查询
- 在Hibernate中使用多表查询的一种实现
- 使用ibatis实现动态分页查询
- hibernate 分页后,查询总数 使用hql和sql
- Mongodb系列- spring-data-mongodb使用MongoTemplate实现分页查询
- hibernate(jpa)根据实体动态生成查询条件,并实现分页问题的解决方案
- mybatis 使用collection标签实现一对多查询(多分页使用)
- 实现Hibernate分页查询原理解读
- 使用Oracle实现的MyBatis分页查询效果
- hibernate3与hibernate4实现分页查询的简单方法
- java web中使用过滤器实现数据查询的分页显示
- Hibernate实现不带条件的简单分页查询
- Hibernate实现分页查询的原理分析