链接:http://gaogengzhi.iteye.com/blog/266301
链接:http://my.oschina.net/lsw90/blog/61816
链接:http://blog.csdn.net/qiyuexuelang/article/details/8894418
链接:http://developer.51cto.com/art/200906/128331.htm
几种查询方法(hql、qbc、qbe、分页,离线等)-链接:http://blog.csdn.net/iijse/article/details/6161143
---参考代码实现:
/** * @function 根据传递过来的Object,分页显示在数据库中与其匹配的记录 * @param pageNo当前页数 * @param pageSize每页显示的记录数 * @param object将查询条件封装为Object * @return 将查询结果封装为Pager返回 */ public Pager findPageByExample(int pageNo, int pageSize, Object object) { Pager pager = null; Criteria criteria = this.getSession().createCriteria(Class.forName(this.getEntity())); criteria.add(Example.create(object).enableLike()); // 获取根据条件分页查询的总行数 int rowCount = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult(); criteria.setProjection(null); criteria.setFirstResult((pageNo - 1) * pageSize); criteria.setMaxResults(pageSize); List result = criteria.list(); pager = new Pager(pageSize, pageNo, rowCount, result); return pager; }
Query q = session.createQuery("from Cat as c"); q.setFirstResult(20000); q.setMaxResults(100); List l = q.list(); //从第2万条开始取出100条记录
/** * 分页查询函数,使用已设好查询条件与排序的Criteria. * * @param pageNo 页号,从1开始. * @return 含总记录数和当前页数据的Page对象. */ public Page pagedQuery(Criteria criteria, int pageNo, int pageSize) { Assert.notNull(criteria); ... String count=criteria.setProjection(Projections.rowCount()).uniqueResult().toString(); int totalCount=Integer.parseInt(count) ;//totalCount ... if (totalCount < 1) return new Page(); int startIndex = Page.getStartOfPage(pageNo, pageSize);//startIndex List list = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list(); return new Page(startIndex, totalCount, pageSize, list); }
概述:Hibernate可通过session的setFirstResult(),setMaxResults()方法执行list()查询,从而达到分页查询的效果。