1 // HQL: Hibernate Query Language. 2 // 特点: 3 // >> 1,与SQL相似,SQL中的语法基本上都可以直接使用。 4 // >> 2,SQL查询的是表和表中的列;HQL查询的是对象与对象中的属性。 5 // >> 3,HQL的关键字不区分大小写,类名与属性名是区分大小写的。 6 // >> 4,SELECT可以省略. 7 8 // 1,简单的查询,Employee为实体名而不是数据库中的表名(面向对象特性) 9 hql = "FROM Employee"; 10 hql = "FROM Employee AS e"; // 使用别名 11 hql = "FROM Employee e"; // 使用别名,as关键字可省略 12 13 // 2,带上过滤条件的(可以使用别名):Where 14 hql = "FROM Employee WHERE id<10"; 15 hql = "FROM Employee e WHERE e.id<10"; 16 hql = "FROM Employee e WHERE e.id<10 AND e.id>5"; 17 18 // 3,带上排序条件的:Order By 19 hql = "FROM Employee e WHERE e.id<10 ORDER BY e.name"; 20 hql = "FROM Employee e WHERE e.id<10 ORDER BY e.name DESC"; 21 hql = "FROM Employee e WHERE e.id<10 ORDER BY e.name DESC, id ASC"; 22 23 // 4,指定select子句(不可以使用select *) 24 hql = "SELECT e FROM Employee e"; // 相当于"FROM Employee e" 25 hql = "SELECT e.name FROM Employee e"; // 只查询一个列,返回的集合的元素类型就是这个属性的类型 26 hql = "SELECT e.id,e.name FROM Employee e"; // 查询多个列,返回的集合的元素类型是Object数组 27 hql = "SELECT new Employee(e.id,e.name) FROM Employee e"; // 可以使用new语法,指定把查询出的部分属性封装到对象中 28 29 // 5,执行查询,获得结果(list、uniqueResult、分页 ) 30 Query query = session.createQuery("FROM Employee e WHERE id<3"); 31 query.setFirstResult(0); 32 query.setMaxResults(10); // 等同于 limit 0,10 33 //两种查询结果list、uniqueResult 34 // List list = query.list(); // 查询的结果是一个List集合 35 // Employee employee = (Employee) query.uniqueResult();// 查询的结果是唯一的一个结果,当结果有多个,就会抛异常 36 37 // 6,方法链 38 List list = session.createQuery(// 39 "FROM Employee e")// 40 .setFirstResult(0)// 41 .setMaxResults(10)// 42 .list(); 43 44 // 7,聚集函数:count(), max(), min(), avg(), sum() 45 hql = "SELECT COUNT(*) FROM Employee"; // 返回的结果是Long型的 46 hql = "SELECT min(id) FROM Employee"; // 返回的结果是id属性的类型 47 48 //8,分组: Group By ... Having 49 hql = "SELECT e.name,COUNT(e.id) FROM Employee e GROUP BY e.name"; 50 hql = "SELECT e.name,COUNT(e.id) FROM Employee e GROUP BY e.name HAVING count(e.id)>1"; 51 hql = "SELECT e.name,COUNT(e.id) FROM Employee e WHERE id<9 GROUP BY e.name HAVING count(e.id)>1"; 52 hql = "SELECT e.name,COUNT(e.id) " + // 53 "FROM Employee e " + // 54 "WHERE id<9 " + // 55 "GROUP BY e.name " + // 56 "HAVING count(e.id)>1 " + // 57 "ORDER BY count(e.id) ASC"; 58 hql = "SELECT e.name,COUNT(e.id) AS c " + // 59 "FROM Employee e " + // 60 "WHERE id<9 " + // 61 "GROUP BY e.name " + // 62 "HAVING count(e.id)>1 " + // 在having子句中不能使用列别名 63 "ORDER BY c ASC"; // 在orderby子句中可以使用列别名 64 65 // 9,连接查询 / HQL是面向对象的查询 66 //>> 内连接(inner关键字可以省略) 67 hql = "SELECT e.id,e.name,d.name FROM Employee e JOIN e.department d"; 68 hql = "SELECT e.id,e.name,d.name FROM Employee e INNER JOIN e.department d"; 69 //>> 左外连接(outer关键字可以省略) 70 hql = "SELECT e.id,e.name,d.name FROM Employee e LEFT OUTER JOIN e.department d"; 71 //>> 右外连接(outer关键字可以省略) 72 hql = "SELECT e.id,e.name,d.name FROM Employee e RIGHT JOIN e.department d"; 73 //可以使用更方便的方法 74 hql = "SELECT e.id,e.name,e.department.name FROM Employee e"; 75 76 // 10,查询时使用参数 77 // >> 方式一:使用‘?‘占位 78 hql = "FROM Employee e WHERE id BETWEEN ? AND ?"; 79 List list2 = session.createQuery(hql)// 80 .setParameter(0, 5)// 设置参数,第1个参数的索引为0。 81 .setParameter(1, 15)// 82 .list(); 83 84 // >> 方式二:使用变量名 85 hql = "FROM Employee e WHERE id BETWEEN :idMin AND :idMax"; 86 List list3 = session.createQuery(hql)// 87 .setParameter("idMax", 15)// 88 .setParameter("idMin", 5)// 89 .list(); 90 91 // 当参数是集合时,一定要使用setParameterList()设置参数值 92 hql = "FROM Employee e WHERE id IN (:ids)"; 93 List list4 = session.createQuery(hql)// 94 .setParameterList("ids", new Object[] { 1, 2, 3, 5, 8, 100 })// 95 .list(); 96 97 // 11,update与delete,不会通知Session缓存 98 // >> Update 99 int result = session.createQuery(// 100 "UPDATE Employee e SET e.name=? WHERE id>15")// 101 .setParameter(0, "无名氏")// 102 .executeUpdate(); // 返回int型的结果,表示影响了多少行。 103 // >> Delete 104 int result1 = session.createQuery(// 105 "DELETE FROM Employee e WHERE id>15")// 106 .executeUpdate(); // 返回int型的结果,表示影响了多少行。
时间: 2024-09-30 06:35:21