对象化查询:
Criteria criteria=session.createCriteria(User.class);
criteria.add(Exception.like("name","J%"));
List users=criteria.list();
面向对象的方式生成SQL
连接 投影 聚合
一 简单查询 在hql语句中 关键字不区分大小写,但是属性和类名是区分大小写的
1.简单属性查询
hql="select name from Student"
返回结果集属性列表,元素类型和实体类中相应的属性类型一致
2 查询多个属性
hql="select id, name from Student"
其集合元素是对象数组
数组元素的类型和对应的属性在实体类中的类型一致
数组的长度取决与select中属性的个数
3 实体对象的查询
如果认为返回数组不够对象化,可以采用hql动态实例化Student对象
必须提供一个空的构造函数和相应的参数的构造函数
hql="select new Student(id, name) from Student"
此时list中为Student对象集合
4 使用别名查询
(1)hql="select s.id, s.name from Student s"
(2)hql="select s.id, s.name from Student as s"
二 实体对象查询
1 返回Student对象的集合
可以忽略select
List students = session.createQuery("from Student").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
2 可以使用别名
List students = session.createQuery("from Student s").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
也可以使用as别名
List students = session.createQuery("from Student as s").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
3 使用select查询实体对象,必须采用别名
List students = session.createQuery("select s from Student as s").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
4 不支持select * from .....这样的查询语句
List students = session.createQuery("select * from Student").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getNamee());
}
5 采用list查询发出一条查询语句,取得Student对象数据
List students = session.createQuery("from Student").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
6 出现N+1问题 1:发出查询id列表的sql, N:在依次发出根据id查询Student对象的sql
N+1问题出现的问题主要是因为迭代接口支持缓存(即sql语句发出后先去session缓存去找)
一定要避免这中情况,因为他很消耗系统性能
Iterator iter = session.createQuery("from Student").iterate();
while(iter.hasNext()) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
7 不会出现N+1问题 因为list操作已经将Student对象放到了一级缓存中,所以再次使用iterate操作的时候
它首先发出一条查询id列表的sql,在根据id到缓存中去数据,只有在缓存中找不到相应的数据时,才会发出sql到数据库中查询
所以 当数据存放在缓存中时,我们可以考虑采用迭代接口,可以提高性能
List students = session.createQuery("from Student").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
System.out.println("-----------------------");
Iterator iter = session.createQuery("from Student").iterate();
while(iter.hasNext()) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
8 再次发出查询sql
* 在默认情况下list每次都会向数据库发出查询对象的sql,除非配置查询缓存,所以下面的list操作
* 虽然在一级缓存中已经有了对象数据,但list默认情况下不会利用缓存,而再次发出sql
* 默认情况下,list会向缓存中放入数据,但不会利用数据
List students = session.createQuery("from Student").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
System.out.println("---------------------------------------------");
students = session.createQuery("from Student").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
三 条件查询
1.可以拼字符串
List students = session.createQuery("select s.id, s.name from Student s where s.name like '%1%'").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
2 可以使用占位符
可以使用?方式传递参数,参数的索引从0开始,传递的参数值,不用单引号引起来
注意方法链编程
List students = session.createQuery("select s.id, s.name from Student s where s.name like ?")
.setParameter(0, "%1%")
.list();
3 使用 :参数名称的方式传递参数值
List students = session.createQuery("select s.id, s.name from Student s where s.name like :myname")
.setParameter("myname", "%1%")
.list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
参数名称 的方式传递参数值
List students = session.createQuery("select s.id, s.name from Student s where s.name like :myname and s.id=:myid")
.setParameter("myname", "%1%")
.setParameter("myid", 12)
.list();
4 支持in,需要使用setParameterList进行参数传递
List students = session.createQuery("select s.id, s.name from Student s where s.id in(:myids)")
.setParameterList("myids", new Object[]{1, 2, 3, 4, 5})
.list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
5 查询2008年2月创建的学生 date_format()为mysql的函数
List students = session.createQuery("select s.id, s.name from Student s where date_format(s.createTime, '%Y-%m')=?")
.setParameter(0, "2008-02")
.list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
6 查询2008-01-10到2008-02-15创建的学生
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List students = session.createQuery("select s.id, s.name from Student s where s.createTime between ? and ?")
.setParameter(0, sdf.parse("2008-01-10 00:00:00"))
.setParameter(1, sdf.parse("2008-02-15 23:59:59"))
.list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
四 hibernate支持原生态sql语句
createSQLQuery...
List students = session.createSQLQuery("select * from t_student").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
五 外置命名查询
SELECT s FROM Student s where s.id
]]>
List students = session.getNamedQuery("searchStudents")
.setParameter(0, 10)
.list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
六 查询过滤器
* 在映射文件中定义过滤器参数
* 在类的映射中使用这些参数
* 在程序中启用过滤器
配置如上:
session.enableFilter("filtertest")
.setParameter("myid", 10);
List students = session.createQuery("from Student").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
七 分页查询
* setFirstResult(),从0开始
* setMaxResults,每页显示多少条数据
List students = session.createQuery("from Student")
.setFirstResult(1)
.setMaxResults(2)
.list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Student student = (Student)iter.next();
System.out.println(student.getName());
}
八 对象导航查询
List students = session.createQuery("select s.name from Student s where s.classes.name like '%1%'").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
String name = (String)iter.next();
System.out.println(name);
}
九 连接查询
* 内连
* 外连接(左连接/右连接)
List students = session.createQuery("select c.name, s.name from Student s inner join s.classes c").list();
for (Iterator iter=students.iterator(); iter.hasNext();) {
Object[] obj = (Object[])iter.next();
System.out.println(obj[0] + "," + obj[1]);
}
List students = session.createQuery("select c.name, s.name from Classes c left join c.students s").list();
List students = session.createQuery("select c.name, s.name from Classes c right join c.students s").list();
十 统计查询
List students =session.createQuery("select count(*) from Student").list();
Long count = (Long)students.get(0);
System.out.println(count);
等价于Long count = (Long)session.createQuery("select count(*) from Student").uniqueResult();
查询每个班级有多少学生
List students =session.createQuery("select c.name, count(s) from Student s join s.classes c " +
"group by c.name order by c.name").list();
十一 DML风格的操作(尽量少用,因为和缓存不同步)批量的删除,修改
session.createQuery("update Student s set s.name=? where s.id < ?")
.setParameter(0, "李四")
.setParameter(1, 5)
.executeUpdate();