HQL(Hibernate Query language)语言

 

现在有两张表:student(学生表),classroom(教室表).

复制代码
//对象 Student 对应 student 表中有四个字段,分别是:id,name,age,classroom; 
public class Student
{
  private int id;
  private String name;
  private int age;
  private Classroom classroom;
}

//对象Classroom 对应 classroom  表中有两个字段:id,name,students;
public class Classroom
{
    private int id;
    private String name;
    private Set<Student> students;
}
复制代码

Student 是一个对象,student 是数据库中的一个表.

查询所有的Student对象时,最简单的HQL语句是: from Student,也可以写成 select s from Student (as) s. 注:这的as可以省略

1.简单的查询遍历对象:

复制代码
 1 // 遍历Student
 2 Query query = session.createQuery("form Student");
 3 // 注: 如果Student对象不是唯一的,那么需要写上包名,如: from test.Student test为包名.
 4 List list = query.list();
 5 for (int i = 0; i < list.size(); i++) {
 6     Student stu = (Student) list.get(i);
 7     System.out.println(stu.getName());
 8 }
 9 // 注意:
10 // 如果执行HQL语句"from Student,Course",并不时单单返回两个对象,而是返回两个对象的笛卡尔积,这类似SQL语句中字段的全外连接.实际的应用中,"from Student,Course"这种语句几乎是不回出现的.
复制代码

2.属性查询:

复制代码
 1 // ----单个属性查询:
 2 Query query = session.createQuery("select s.name form Student s");
 3 List list = query.list();
 4 for (int i = 0; i < list.size(); i++) {
 5     String name = (String) list.get(i);
 6     System.out.println(name);
 7 }
 8 
 9 // ----多个属性查询:
10 Query query = session.createQuery("select s.name,s.age form Student s");
11 List list = query.list();
12 for (int i = 0; i < list.size(); i++) {
13     Object obj[] = (Object[]) list.get(i); // 取得list中的第i个对象
14     System.out.println(obj[0] + "的年龄为: " + obj[1]);
15 }
复制代码

3.实例化查询:

复制代码
 1 // 实例化查询结果可以说是对属性查询的一重改进.在使用属性查询时由于使用对象数组,操作和理解不太方便,
 2 // 如果将以个Object[]中的成员封装成一个对象就方便多了.
 3 Query query = session.createQuery("select new Student(s.name,s.age) form Student s");
 4 List list = query.list();
 5 for (int i = 0; i < list.size(); i++) {
 6     Student stu = (Student) list.get(i);
 7     System.out.println(stu.getName());
 8 }
 9 
10 // 注:运行这个程序的时候,需要一个new Student(s.name,s.age)构造函数.在Student.java中编写这个构造函数.
11 public Student(String name, int age) {
12     this.name = name;
13     this.age = age;
14 }
复制代码

4.动态参数查询:

  1)、基于 ? 的参数形式

复制代码
 1 /*
 2  * setParameter的方式可以防止sql注入 jdbc的setParameter的下标从1开始,hql的下标从0开始
 3  */
 4 Query query = session.createQuery("select stu from Student stu where name like ? ");
 5 query.setParameter(0, "%张三%");
 6 List list = query.list();
 7 for (int i = 0; i < list.size(); i++) {
 8     Student stu = (Student) list.get(i);
 9     System.out.println(stu.getName());
10 }
复制代码

  2)、基于 :xx 的别名参数形式

复制代码
 1 /*
 2  * 通过 :xxx 格式设置别名
 3  */
 4 Query query = session.createQuery("select stu from Student stu where name like :name ");
 5 query.setParameter("name","%李四%");
 6 List list = query.list();
 7 for (int i = 0; i < list.size(); i++) {
 8     Student stu = (Student) list.get(i);
 9     System.out.println(stu.getName());
10 }
复制代码

  注意:如果返回结果只有一个,可以在 Query 后使用 uniqueResult 方法  e.g.: query.uniqueResult(); 

      in 查询: "from Student where id in (:ids)" 传入参数使用 query.setParameterList("ids", new Integer[] { 1, 2 }); 

5.分页查询:

  在 Query 后 使用 setFirstResult(x) 方法设置从第x条开始取,使用 setMaxResults(y) 方法设置取出y条数据  query.setFirstResult(0).setMaxResults(10); 

6.连表查询:

1: inner jion (内连接); inner jion 可以简写为join
2: left outer join (左外连接)
3: right outer join(右外连接)
4: full join(全连接--不常用)

复制代码
 1 /*
 2  * inner join  
 3  * sql 与 hql 区别:
 4  * sql: select s.* from Student s join Classroom c on s.cid=c.id
 5  * hql: select s from Student s join s.classroom room
 6  */
 7 Query query = session.createQuery("select s from Student s join s.classroom room where room.id=2");
 8 List list = query.list();
 9 for (int i = 0; i < list.size(); i++) {
10     Student stu = (Student) list.get(i);
11     System.out.println(stu.getName());
12 }
复制代码

7.聚合函数查询:

复制代码
 1 //1: count() 统计记录的条数
 2 //2: min() 求最小值
 3 //3: max() 求最大值
 4 //4: sum() 求和
 5 //5: avg() 求平均值
 6 // 取得Student的数量
 7 Query query = session.createQuery("select count(*) from Student");
 8 
 9 // avg()取得Student平均年龄
10 query = session.createQuery("select avg(s.age) from Student as s");
11 
12 // upper()方法将字符串转为大写
13 query = session.createQuery("select upper(s.name) from Student as s");
14 
15 // 去除重复行distinct
16 query = session.createQuery("select distinct s.age from Student as s");
复制代码

8.子查询:

复制代码
 1 //all 表示所有记录
 2 //any 便是所有记录中的任意一条
 3 //somy 与any用法一样
 4 //in 等价于any
 5 //exists 表示子查询至少要返回一条数据.
 6 
 7 //all:
 8 from Team t where 22 < all(select s.age from Student s)
 9 
10 from Team t where all(select s.age from t.student s) > 22
复制代码

9.修改update()

1 Student stu=(Student)session.get(Student.class,"id");      //根据id 得到stu对象
2 stu.setName("123");
3 session.update(stu);

10.删除:delete()

1 Student stu=(Student)session.get(Student.class,"id");      //根据id 得到stu对象
2 session.delete(stu);
原文地址:https://www.cnblogs.com/laobiao/p/5446733.html