关于hibernate中聚合函数的用法
1. public int countDetails() throws Exception {
// TODO Auto-generated method stub
Transaction trans = null;
trans = HibernateSessionFactory.getSession().beginTransaction();
int m =(Integer)HibernateSessionFactory.getSession().createQuery(
"select count(*) from Details").list().get(0);
trans.commit();
HibernateSessionFactory.closeSession();
return m;
}
返回数据总行数
2.public double countDetailsMoney() throws Exception {
Transaction trans = null;
trans = HibernateSessionFactory.getSession().beginTransaction();
Double m =(Double)HibernateSessionFactory.getSession().createQuery(
"select sum(money) from Details").list().get(0);
trans.commit();
HibernateSessionFactory.closeSession();
return m;
}
计算总金额
本文提供了两个使用Hibernate框架执行数据库聚合函数的例子:一是获取表中所有记录的数量;二是计算某个字段(如金钱金额)的总和。通过这两个例子,读者可以了解到如何在Hibernate中运用SQL的COUNT(*)和SUM()函数。

1163

被折叠的 条评论
为什么被折叠?



