HQL

1:带参数的查询

 1     @org.junit.Test
 2     public void testHql(){
 3         //创建一个query对象。
 4         //查询的是类名 , 类的属性名。
 5         String hql="from Employee e where e.salary>? and e.name=?"; 
 6         Query query =session.createQuery(hql);
 7         //设置参数。
 8         //set后面是  属性的类型 float。
 9         query.setFloat(0, 2500)
10              .setString(1, "SCOTT");
11             
12         //执行查询
13         List<Employee> emps=query.list();
14         System.out.println(emps.size());
15     }

2:分页查询

 @org.junit.Test
    public void testFenye(){
        String hql="from Employee e";
        Query query=session.createQuery(hql);
        int pageNo=query.list().size()/7; //这设置的是第几页
        int pageSize=2; //设置的是每页显示几条数据
        
        List<Employee> emps=
                            query.setFirstResult((pageNo-1)*pageSize) //设定从哪一个开始查询。
                                 .setMaxResults(pageSize)//设定一次最多检索的数据。
                                 .list();
        System.out.println(emps);
    }

3:命名查询语句

在employee的映射文件中设置:

<query name="salEmps"><![CDATA[FROM Employee e WHERE e.salary > :minSal AND e.salary < :maxSal]]></query>
1 @org.junit.Test
2     public void testSal(){
      //注意。用的是getNamedQuery方法。
3 Query query=session.getNamedQuery("salEmps"); 4 List<Employee> emps= 5 query.setFloat("minSal", 1000) 6 .setFloat("maxSal", 2000).list(); 7 System.out.println(emps.size()); 8 }

4:投影查询

 1 /** 
 2      * 投影查询: 查询结果仅包含实体的部分属性. 通过 SELECT 关键字实现. 
 3      *  
 4      *      1. Query 的 list() 方法返回的集合中包含的是数组类型的元素,  
 5      *          每个对象数组代表查询结果的一条记录 
 6      *          String hql = "SELECT e.dept, e.email,e.salary FROM Employee e WHERE e.dept = :dept"; 
 7      *          Query query = session.createQuery(hql); 
 8      * 
 9      *          Department dept = new Department(); 
10      *          dept.setId(80); 
11      *           
12      *          List<Object[]> result = query.setEntity("dept", dept).list(); 
13      *          for(Object[] objs:result ){ 
14      *              System.out.println(Arrays.asList(objs)); 
15      *          } 
16      * 
17      *      2. 可以在持久化类中定义一个对象的构造器来包装投影查询返回的记录,  
18      *          使程序代码能完全运用面向对象的语义来访问查询结果集. 
19      *          String hql = "SELECT new Employee(e.dept, e.email,e.salary)" 
20      *                              + "FROM Employee e " 
21      *                              + "WHERE e.dept = :dept"; 
22      *          Query query = session.createQuery(hql); 
23      * 
24      *          Department dept = new Department(); 
25      *          dept.setId(80); 
26      *           
27      *          List<Employee> result = query.setEntity("dept", dept).list(); 
28      *          for(Employee emp:result ){ 
29      *              System.out.println(emp.getDept()+ ", " + emp.getEmail()+ ", " +  emp.getSalary()); 
30      *          } 
31      * 
32      *      3. 可以通过 DISTINCT 关键字来保证查询结果不会返回重复元素 
33      * 
34      */  
35     @Test  
36     public void testPropertyQuery(){  
37         String hql = "SELECT new Employee(e.dept, e.email,e.salary)"  
38                     + "FROM Employee e "  
39                     + "WHERE e.dept = :dept";  
40         Query query = session.createQuery(hql);  
41           
42         Department dept = new Department();  
43                    dept.setId(80);  
44                       
45         List<Employee> result = query.setEntity("dept", dept).list();  
46               
47         for(Employee emp:result ){  
48             System.out.println(emp.getDept()+ ", " + emp.getEmail()+ ", " +  emp.getSalary());  
49         }  
50       
51     }  
52       

4:报表查询

  /** 
     * 报表查询 
     *  
     * 1. 报表查询用于对数据分组和统计, 与 SQL 一样, HQL 利用 GROUP BY 关键字对数据分组,  
     *      用 HAVING 关键字对分组数据设定约束条件. 
     *   
     * 2. 在 HQL 查询语句中可以调用以下聚集函数 
     *      count() 
     *      min() 
     *      max() 
     *      sum() 
     *      avg() 
     */  
    @Test  
    public void testGroupBy(){  
        String hql = "SELECT min(e.salary),max(e.salary) "  
                    + "FROM Employee e "  
                    + "Group BY e.dept "  
                    + "Having min(salary) > :minSal";  
        Query query = session.createQuery(hql)  
                             .setFloat("minSal", 5000)  
                             ;  
          
        List<Object[]> result = query.list();  
        for(Object[] objs:result ){  
            System.out.println(Arrays.asList(objs));  
        }     
    }  

5:HQL的删除和更新

 1  // HQL 的删除  
 2     @Test  
 3     public void testHQLDelete(){  
 4           
 5         String hql = "DELETE FROM Department d WHERE d.id = :id";  
 6         Query query = session.createQuery(hql);  
 7           
 8           
 9         query.setFloat("id", 280)  
10              .executeUpdate();  
11     }  
12       
13     // HQL 的更新  
14     @Test  
15     public void testHQLUpdate(){  
16         String hql = "UPDATE Department d SET d.name = ? WHERE d.id = ?";  
17         Query query = session.createQuery(hql);  
18           
19         query.setText(0, "ChuckHero")  
20              .setFloat(1, 280)  
21              .executeUpdate();  
22     }  
23       
24 }  
原文地址:https://www.cnblogs.com/bulrush/p/7841595.html