HQL(Hibernate Query language)语言

时间:2023-03-08 17:34:31

现在有两张表: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.简单的查询遍历对象:

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

2.属性查询:

 // ----单个属性查询:
Query query = session.createQuery("select s.name form Student s");
List list = query.list();
for (int i = 0; i < list.size(); i++) {
String name = (String) list.get(i);
System.out.println(name);
} // ----多个属性查询:
Query query = session.createQuery("select s.name,s.age form Student s");
List list = query.list();
for (int i = 0; i < list.size(); i++) {
Object obj[] = (Object[]) list.get(i); // 取得list中的第i个对象
System.out.println(obj[0] + "的年龄为: " + obj[1]);
}

3.实例化查询:

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

4.动态参数查询:

1)、基于 ? 的参数形式

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

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

 /*
* 通过 :xxx 格式设置别名
*/
Query query = session.createQuery("select stu from Student stu where name like :name ");
query.setParameter("name","%李四%");
List list = query.list();
for (int i = 0; i < list.size(); i++) {
Student stu = (Student) list.get(i);
System.out.println(stu.getName());
}

注意:如果返回结果只有一个,可以在 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(全连接--不常用)

 /*
* inner join
* sql 与 hql 区别:
* sql: select s.* from Student s join Classroom c on s.cid=c.id
* hql: select s from Student s join s.classroom room
*/
Query query = session.createQuery("select s from Student s join s.classroom room where room.id=2");
List list = query.list();
for (int i = 0; i < list.size(); i++) {
Student stu = (Student) list.get(i);
System.out.println(stu.getName());
}

7.聚合函数查询:

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

8.子查询:

 //all 表示所有记录
//any 便是所有记录中的任意一条
//somy 与any用法一样
//in 等价于any
//exists 表示子查询至少要返回一条数据. //all:
from Team t where 22 < all(select s.age from Student s) from Team t where all(select s.age from t.student s) > 22

9.修改update()

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

10.删除:delete()

 Student stu=(Student)session.get(Student.class,"id");      //根据id 得到stu对象
session.delete(stu);