hibernate分页查询的各种方法
统计总数:
public Integer countAll1() { String hql = "select count(*) from News as news"; List list = getHibernateTemplate().find(hql); return list.get(0).hashCode(); } public Integer countAll2() { Criteria criteria = this.getSession().createCriteria(News.class); return ((Integer) (criteria.setProjection(Projections.rowCount())).uniqueResult()).intValue(); }
分页查询:
public List pagerList(Integer beginNum,Integer maxNum){ DetachedCriteria dCriteria = DetachedCriteria.forClass(News.class); List list = getHibernateTemplate().findByCriteria(dCriteria, beginNum, maxNum); return list; } public List pagerList1(String news,Integer beginNum,Integer maxNum){ Criteria criteria = this.getSession().createCriteria(News.class); criteria.setFirstResult(beginNum); criteria.setMaxResults(maxNum); return criteria.list(); } public List pagerList2(String news,Integer beginNum,Integer maxNum){ String hql = "from News"; Query query = this.getSession().createQuery(hql); query.setFirstResult(beginNum); query.setMaxResults(maxNum); return query.list(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。