Hibernate(分组查询等续)
2013-04-27 14:28
197 查看
投影查询
使用聚集函数
在HQL中可以调用
Count:统计函数
Min:求最小值函数
Max:求最大值函数
Sum:求和函数
Avg:求平均数函数
Count:统计函数
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Integer count = (Integer)session.createQuery("select count(*) from Hx").uniqueResult();
System.out.print(count);
tx.commit();
session.close();
Avg:求平均数函数
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Float count = (Float)session.createQuery("select avg(c.id) from Hx c").uniqueResult();
System.out.print(count);
tx.commit();
session.close();
Sum:求和函数
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Integer count = (Integer)session.createQuery("select sum(c.id) from Hx c").uniqueResult();
System.out.print(count);
tx.commit();
session.close();
Min:求最小值函数 Max:求最大值函数
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Object[] count = (Object[])session.createQuery("select min(c.age),max(c.age) from Hx c").uniqueResult();
String min = (String)count[0];
String max = (String)count[1];
System.out.print("min="+min+"|max="+max);
tx.commit();
session.close();
分组查询
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Iterator it = session.createQuery("select c.name,count(c) from Hx c group by c.name").iterate();
while(it.hasNext())
{
Object[] oc = (Object[])it.next();
String count = (Integer)oc[1];
System.out.println(name+":"+count);
}
tx.commit();
session.close();
使用聚集函数
在HQL中可以调用
Count:统计函数
Min:求最小值函数
Max:求最大值函数
Sum:求和函数
Avg:求平均数函数
Count:统计函数
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Integer count = (Integer)session.createQuery("select count(*) from Hx").uniqueResult();
System.out.print(count);
tx.commit();
session.close();
Avg:求平均数函数
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Float count = (Float)session.createQuery("select avg(c.id) from Hx c").uniqueResult();
System.out.print(count);
tx.commit();
session.close();
Sum:求和函数
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Integer count = (Integer)session.createQuery("select sum(c.id) from Hx c").uniqueResult();
System.out.print(count);
tx.commit();
session.close();
Min:求最小值函数 Max:求最大值函数
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Object[] count = (Object[])session.createQuery("select min(c.age),max(c.age) from Hx c").uniqueResult();
String min = (String)count[0];
String max = (String)count[1];
System.out.print("min="+min+"|max="+max);
tx.commit();
session.close();
分组查询
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
Iterator it = session.createQuery("select c.name,count(c) from Hx c group by c.name").iterate();
while(it.hasNext())
{
Object[] oc = (Object[])it.next();
String count = (Integer)oc[1];
System.out.println(name+":"+count);
}
tx.commit();
session.close();
相关文章推荐
- Hibernate基本查询下篇:Hibernate聚合函数、分组查询及命名查询 5
- hibernate HQL查询语句分组、排序
- hibernate学习系列-----(5)hibernate基本查询下篇:hibernate聚合函数、分组查询及命名查询
- hibernate分组查询
- 【Hibernate】Hibernate的聚类查询、分组查询、排序与时间之差
- Hibernate的查询 HQL查询 参数,分页,分组
- Hibernate 分组查询 子查询 原生SQL
- Hibernate HQL 分组求和查询
- 请教一下hibernate中的criteria分组查询语句报类转换错误,出了什么问题?
- hibernate--HQL查询语句-----分组、排序--多条件排序
- Hibernate的查询 标准(Criteria)查询 聚合和分组
- hibernate 分组查询
- Hibernate(分组查询等续)
- Hibernate的查询 标准(Criteria)查询 聚合和分组
- hibernate--HQL查询语句-----分组、排序--多条件排序
- hibernate分组查询
- hibernate--HQL查询语句(二)-----分组、排序
- hibernate HQL查询语句分组、排序
- Hibernate5-投影查询,分组查询,Query的List和Iterate
- hibernate系列十六:分组查询,子查询,原生sql查询,命名查询