Hibernate(七)

三套查询之HQL查询(原文再续书接上一回)

where子句部分(查询过滤部分)

Hibernate的where子句部分能支持的运算符,表达式、函数特别多,用法与sql语句是一样的.
常用的表达式、运算符、函数:
=、<、<=、>、>=、!=、and、or、distinct、between...and 、like、concat()、
is null, is not null, is empty, is not empty、second(...),
minute(...), hour(...), day(...), month(...)。
支持EJB-QL 3.0的函数:
trim(), lower(), upper(), length(), abs(), sqrt(), bit_length(), mod()
支持操作集合属性的函数:
size()|size, minelement(), maxelement(), minindex(), maxindex().

1.支持自定义的索引号

 1     //支持自定义的索引号
 2     @Test
 3     public void test5(){
 4         Query query = session.createQuery("select s from Student s where s.name like ?57");
 5         query.setParameter("57", "%ab%");
 6         List<Student> list = query.list();
 7         for (Student student : list) {
 8             System.out.println(student.getId()+student.getName()+student.getTeacher().getTname());
 9         }
10     }

 2.like

 1        //like
 2         @Test
 3         public void test6(){
 4             Query query = session.createQuery("select s from Student s where s.name like ?3");
 5             query.setParameter("3", "%k%");
 6             List<Student> list = query.list();
 7             for (Student student : list) {
 8                 System.out.println(student.getId()+student.getName()+student.getTeacher().getTname());
 9             }
10         }

3.between and

 1     //between and
 2     @Test
 3     public void test7(){
 4         Query query = session.createQuery("select s from Student s where s.name like ?3 and s.java between ?4 and ?5");
 5         query.setParameter("3", "%k%").setParameter("4", 10).setParameter("5", 80);
 6         List<Student> list = query.list();
 7         for (Student student : list) {
 8             System.out.println(student.getId()+student.getName()+student.getJava()+student.getTeacher().getTname());
 9         }
10     }    

4.concat

 1     //concat
 2     @Test
 3     public void test8(){
 4         Query query = session.createQuery("select s from Student s where s.name like concat('%',?,'%')");
 5         query.setParameter(0, "ab");
 6         List<Student> list = query.list();
 7         for (Student student : list) {
 8             System.out.println(student.getId()+student.getName()+student.getTeacher().getTname());
 9         }
10     }    

5.is not null

1     //is not null
2     @Test
3     public void test9(){
4         Query query = session.createQuery("select s from Student s where s.name is not null");
5         List<Student> list = query.list();
6         for (Student student : list) {
7             System.out.println(student.getId()+student.getName()+student.getTeacher().getTname());
8         }
9     }    

6.trim

 1     //trim
 2     @Test
 3     public void test10(){
 4         Query query = session.createQuery("select s from Student s where trim(s.name) like ?");
 5         query.setParameter(0, "aa%");
 6         List<Student> list = query.list();
 7         for (Student student : list) {
 8             System.out.println(student.getId()+student.getName()+student.getTeacher().getTname());
 9         }
10     }    

7.upper

 1     //upper
 2     @Test
 3     public void test11(){
 4         Query query = session.createQuery("select upper(s.name) from Student s where s.name like ?");
 5         query.setParameter(0, "%b%");
 6         List<String> list = query.list();
 7         for (String string : list) {
 8             System.out.println(string);
 9         }
10     }    

8.length

 1     //length
 2     @Test
 3     public void test12(){
 4         Query query = session.createQuery("select s from Student s where length(s.name)>? ");
 5         query.setParameter(0, 4);
 6         List<Student> list = query.list();
 7         for (Student student : list) {
 8             System.out.println(student.getId()+student.getName()+student.getTeacher().getTname());
 9         }
10     }        

9.sqrt

 1     //sqrt
 2     @Test
 3     public void test13(){
 4         Query query = session.createQuery("select s from Student s where sqrt(s.id)=?");
 5         query.setParameter(0, 2.0);
 6         List<Student> list = query.list();
 7         for (Student student : list) {
 8             System.out.println(student.getId()+student.getName()+student.getTeacher().getTname());
 9         }
10     }    

10.bit_length

1     //bit_length 位长度
2     @Test
3     public void test14(){
4         Query query = session.createQuery("select bit_length(s.name) from Student s where s.id=?57");
5         query.setParameter("57", 3);
6         Long uniqueResult = (Long) query.uniqueResult();
7         System.out.println(uniqueResult);
8     }

11.mod

 1     //mod
 2     @Test
 3     public void test15(){//选取id为偶数的学生
 4         Query query = session.createQuery("select s from Student s where mod(s.id,?)=?");
 5         query.setParameter(0, 2);
 6         query.setParameter(1, 0);
 7         List<Student> list = query.list();
 8         for (Student student : list) {
 9             System.out.println(student.getId()+student.getName());
10         }
11     }

12.size

操作集合属性的函数

 1     //size
 2     @Test
 3     public void test16(){
 4         Query query = session.createQuery("select t from Teacher t where size(t.students)>?");
 5         query.setParameter(0, 50);
 6         List<Teacher> list = query.list();
 7         for (Teacher teacher : list) {
 8             System.out.println(teacher.getTid()+teacher.getTname());
 9         }
10         System.out.println("======================================");
11         Query query1 = session.createQuery("select t from Teacher t where t.students.size>?");
12         query1.setParameter(0, 30);
13         List<Teacher> list1 = query1.list();
14         for (Teacher teacher : list1) {
15             System.out.println(teacher.getTid()+teacher.getTname());
16         }
17     }

13.minelement(), maxelement()

对集合中元素生成的列进行操作

 

 1     //minelement(), maxelement()  返回集合中的元素主键最大/最小的元素
 2     @Test
 3     public void test17(){
 4         Query query = session.createQuery("select maxelement(t.students) from Teacher t");
 5         List<Student> list = query.list();
 6         for (Student student : list) {
 7             System.out.println(student.getId()+":"+student.getName()+":"+student.getJava());
 8         }
 9         System.out.println("===============================");
10         Query query1 = session.createQuery("select minelement(t.students) from Teacher t");
11         List<Student> list1 = query1.list();
12         for (Student student : list1) {
13             System.out.println(student.getId()+":"+student.getName()+":"+student.getJava());
14         }
15     }

 

14.minindex(), maxindex()

对集合中的排序列进行操作。

对于索引了(有序)的集合,可以使用minindex 与 maxindex函数来引用到最小与最大的索引序数。

同理,可以使用minelement 与 maxelement函数来 引用到一个基本数据类型的集合中最小与最大的元素。(如上)

List<Order> orders = session.createQuery("select o from Order o where minindex(o.orderItems) = ?").setParameter(0,0).list();

List<Order> orders = session.createQuery("select o from Order o where maxindex(o.orderItems) = ?").setParameter(0,0).list();

子查询

Hibernate的子查询与SQL语句中的子查询一样,子查询部分放在in、not in里面.

1     //把老师的id和学生的id一样的学生查询出来
2     @Test
3     public void test19(){
4         Query query = session.createQuery("select s from Student s where s.id in (select t.tid from Teacher t)");
5         List<Student> list = query.list();
6         for (Student student : list) {
7             System.out.println(student.getId()+student.getName()+student.getTeacher().getTname());
8         }
9     }

多态查询

当持久化类存在继承关系时,查询父类时,它会把父类所有的对象查询出来,而且也会把所有子类对象查询出来。

1     //查询所有
2     @Test
3     public void test20(){
4         //Query query = session.createQuery("select o from java.lang.Object o");
5         Query query = session.createQuery("from java.lang.Object");
6         List list = query.list();
7         System.out.println(list.size());
8     }

命名查询

把所有hql语句写在一个单独的配置文件中.
一般在实际的项目中用得比较多,它会把比较复杂的hql语句写在一个单独的配置文件中,
方便以后对hql语句进行优化,也方便统一管理.

1.第一种方式*.hbm.xml

A.创建Query.hbm.xml

在官方下载的项目里搜索*.hbm.xml,拷贝一个文件到自己的项目里,修改文件名(任意),修改文件内容:

<!-- 定义HQL查询语句 -->

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <query name="hql_student_xml">from Student</query>
</hibernate-mapping>

 

 

 

B.配置到 hibernate.cfg.xml

 

C.代码使用

1     @Test
2     public void test21(){
3         Query query = session.getNamedQuery("hql_student_xml");
4         List<Student> list = query.list();
5         for (Student student : list) {
6             System.out.println(student.getId()+student.getName());
7         }
8     }

2.第二种方式在持久类上加注解

 1 package com.rong.entity.group;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.FetchType;
 5 import javax.persistence.GeneratedValue;
 6 import javax.persistence.GenerationType;
 7 import javax.persistence.Id;
 8 import javax.persistence.JoinColumn;
 9 import javax.persistence.ManyToOne;
10 import javax.persistence.NamedQueries;
11 import javax.persistence.NamedQuery;
12 
13 @Entity
14 //配置多个命名查询hql语句
15 @NamedQueries(value={@NamedQuery(name="query1",query="select s from Student s where s.name like ?")
16                     ,@NamedQuery(name = "query2", query = "from Student")})
17 //只能配置单个命名查询hql语句,一个类中该注解只能使用一次!
18 @NamedQuery(name = "query3", query = "select count(s) from Student s")
19 public class Student {
20     @Id
21     @GeneratedValue(strategy=GenerationType.AUTO)
22     private int id;
23     private String name;
24     private int math;
25     private int java;
26     @ManyToOne(targetEntity=Teacher.class,fetch=FetchType.LAZY)
27     @JoinColumn(name="t_id",referencedColumnName="tid")
28     private Teacher teacher;
29     public int getId() {
30         return id;
31     }
32     public void setId(int id) {
33         this.id = id;
34     }
35     public String getName() {
36         return name;
37     }
38     public void setName(String name) {
39         this.name = name;
40     }
41     public int getMath() {
42         return math;
43     }
44     public void setMath(int math) {
45         this.math = math;
46     }
47     public int getJava() {
48         return java;
49     }
50     public void setJava(int java) {
51         this.java = java;
52     }
53     public Teacher getTeacher() {
54         return teacher;
55     }
56     public void setTeacher(Teacher teacher) {
57         this.teacher = teacher;
58     }
59 }
 1     @Test
 2     public void test22(){
 3         Query query1 = session.getNamedQuery("query1");
 4         query1.setParameter(0, "%ab%");
 5         List<Student> list1 = query1.list();
 6         for (Student student : list1) {
 7             System.out.println(student.getId()+student.getName());
 8         }
 9         
10         Query query2 = session.getNamedQuery("query2");
11         List<Student> list2 = query2.list();
12         for (Student student : list2) {
13             System.out.println(student.getId()+student.getName());
14         }
15         
16         Query query3 = session.getNamedQuery("query3");
17         Object uniqueResult = query3.uniqueResult();
18         System.out.println(uniqueResult);
19     }

 

  

posted @ 2018-01-15 20:11  57容杰龙  阅读(199)  评论(0编辑  收藏  举报