Hibernate中如何使用count(*)取得表中记录总数

spring+hibernate

//第一种方法:
  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();

你可能感兴趣的:(spring,Hibernate)