Hibernate:HQLQBC查询语言比较的用法(2)

    技术2022-05-11  147

    过滤查询结果中的重复元素       使用Set集合来去除重复元素;或是使用distinct元素      Iterator iterator = session.createQuery("select distinct c.name from customer ").list().iterator();      while(iterator.hasnext()){            String name = (String) it.next() ;      } ///使用聚集函数 count(); 记录的条数 min();   求最小值 max();   求最大值 avg();   求平均值 sum();   求和 1'查询customer中的所有记录条数    integer i = (Integer) session.createQuery("select count(*) from customer").uniqueResult(); 2'查询customer中的所有客户的平均年龄    integer i = (Integer) session.createQuery("select avg(c.age) from customer c ").uniqueResult(); 3'查询customer中的客户年龄的最大值、最小值    object [] i = (Integer) session.createQuery("select max(c.age),min(c.age) from customer c ").uniqueResult();    Integer Maxage = (Integer) i [0];    Integer Minage = (Integer) i [1]; 4'统计customer中的客户的名称数目,忽略重复的姓名    Integer cout = (Integer) session.createQuery("select count(distinct c.name) from customer c").uniqueResult(); 使用分组查询 1'按姓名分组,统计customer中的相同姓名的记录数目    Iterator iterator = (Integer) session.createQuery("select c.name ,count(c) from customer c group by c.name").list.iterator();    while(iterator.hasnext()){          object[] p = (objcet[])iterator.next();          String name = p[0];          Integer cout = p[1];    } 2'按客户分组,统计每个客户的订单数量    Iterator iterator = session.crateQuery("select c.id ,c.name , count(o) from customer c join c.order o group by c.id ").list().iterator;    while(iterator.hasnext()){          object[] p = (objcet[])iterator.next();          Integer id = p[0]          String name = p[1];          Integer cout = p[2];    } 3'统计每个客户的订单总价    Iterator iterator = session.crateQuery("select c.id ,c.name,sum(o.price) from customer c join c.order o group by c.id").list.iterator();    while(iterator.hasnext()){          object[] p = (objcet[])iterator.next();          Integer id = p[0]          String name = p[1];          Double cout = p[2];    } //报表的优化 使用HQL时如果只查询对象的一部分属性不会返回持久化对象 from customer c join c.order o group by c.age;//返回持久化对象,占用缓存。 select c.id , c.name , c.age ,o.id ,o.price from customer c join c.order o group by c.age //返回关系性数据,不占用session的缓存,可以为JVM回收 /HQL子查询的集合函数属性 size() 返回集合中元素的数目 minIndex() 建立索引的集合获得最小的索引 maxIndex() 建立索引的集合获得最大的索引 minElement() 对于包含基本元素的集合获得集合最小的元素 maxElement() 对于包含基本元素的集合获得集合最大的元素 element() 获得集合中的所有元素 ///Hibernate的缓存管理 管理一级缓存,不建议使用下列方法来管理一级缓存 evict(Object o)从缓存中清除参数指定的持久化对象 clear()清空缓存中所有的持久化对象 ///批量更新和批量删除 批量更新customer表中的年龄大于零的所有记录的AGE字段:     tx = session.beginTransaction();     Iterator customer = session.find("from customer c where c.age > 0").iterator();     while(iterator.hasnext()){         customer customer = (customer) customer.next();         customer.setAge(customer.getAge()+1);     }     tx.commit();     session.close();//上述代码对数据库操作效果不好可以使用下列的JDBCAPI直连         tx = session.beginTranscation();     Connection conn = session.connection();     Preparedstatement ps = conn.preparedstatment("update customer set age = age + 1 where age > 0 ");     ps.executeupdate();     tx.commit();

    最新回复(0)