hql语言

2022-08-09 21:46:01 浏览数 (3)

大家好,又见面了,我是你们的朋友全栈君。

HQL(Hibernate Query Language)跟我们以前用的SQL有很多相似之处,但是SQL查询的是表和表中的列;HQL查询的是对象与对象中的属性。HQL的关键字不区分大小写,类名与属性名是区分大小写的。SELECT可以省略。

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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/105712.html原文链接:https://javaforall.cn

0 人点赞