hibernate总记录数查询和分页查询

时间:2021-06-21 13:44:53
 //参考代码
//第一种方法:
String hql = "select count(*) from User as user";
Integer count = (Integer)getHibernateTemplate().find(hql).listIterator().next();
return count.intValue(); //第二种方法:
String hql = "select count(*) from User as user";
return ((Integer)getHibernateTemplate().iterate(hql).next()).intValue(); //第三种方法:
String hql = "select count(*) from User as user";
Query query = getHibernateTemplate().createQuery( getSession(),hql);
return ((Integer)query.uniqueResult()).intValue(); //参考代码
//第一种方法:
String hql = "select count(*) from User as user";
Integer count = (Integer)getHibernateTemplate().find(hql).listIterator().next();
return count.intValue(); //第二种方法:
String hql = "select count(*) from User as user";
return ((Integer)getHibernateTemplate().iterate(hql).next()).intValue(); //第三种方法:
String hql = "select count(*) from User as user";
Query query = getHibernateTemplate().createQuery( getSession(),hql);
return ((Integer)query.uniqueResult()).intValue(); ================================================================
@Override
public int getTotalRecord(int webId) {
String hql = "SELECT COUNT(a.id) FROM com.babycms.pojo.TArticle a where a.siteId=" + webId;
return ((Long) getHibernateTemplate().iterate(hql).next()).intValue();
}

分页查询

    public List<TArticle> getArticleByWebId(final int webId, final int start, final int maxSize) {

        return getHibernateTemplate().executeFind(new HibernateCallback(
){ @Override
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
String hql = "FROM com.babycms.pojo.TArticle a where a.siteId=" + webId;
Query q = session.createQuery(hql);
q.setFirstResult(start);
q.setMaxResults(maxSize);
return q.list();
}
});
}

参考:http://www.jiancool.com/article/93763502098/;jsessionid=0CF84AC7703AA46B31BAE5406DCA4FC9