聚集函数使用
1 常用的聚集函数
(1)count、sum、avg、max、min
2 hql聚集函数语句写法
(1)查询表记录数
- select count(*) from 实体类名称
1 常用的聚集函数
(1)count、sum、avg、max、min
2 hql聚集函数语句写法
(1)查询表记录数
- select count(*) from 实体类名称
//聚集函数使用
@Test
public void testSelect7(){
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx = null;
try{
sessionFactory = HibernateUtils.getSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
//1 创建query对象
Query query = session.createQuery("select count(*) from Customer");
Object object = query.uniqueResult();
//首先把object变成long类型,再变成int类型
Long l = (Long) object;
int count = l.intValue();
System.out.println(count);
tx.commit();
}catch(Exception e){
tx.rollback();
}finally{
session.close();
sessionFactory.close();
}
}