//使用hibernate,实现group by and sum and count
Session sess = this.getSession(false);
List list = null;
if (sess != null) {
Criteria cri = sess.createCriteria(getModelClass());
cri.add(Expression.allEq(props));
// always count id
ProjectionList projList = Projections.projectionList();
projList.add(Projections.sum(sum));
projList.add(Projections.groupProperty(group1));
projList.add(Projections.groupProperty(group2));
projList.add(Projections.count(count));
cri.setProjection(projList);
list = cri.list();
}
list = list == null ? new ArrayList() : list;
return list;
//使用hibernate,实现group by and sum and count
List listByGroupSum = dao.getListByGroupSumCP(props);
Iterator iter = listByGroupSum.iterator();
if (!iter.hasNext()) {
System.out.println("No objects to display.");
}
while (iter.hasNext()) {
System.out.println("New object");
Object[] obj = (Object[]) iter.next();
for (int i = 0; i < obj.length; i++) {
System.out.println(obj[i]);
}
}
本文介绍如何使用Hibernate实现数据的分组及统计功能,包括求和(sum)、计数(count)等操作。通过具体示例代码展示了如何设置投影、创建查询条件并获取结果集。
1431

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



