首页 > Web开发 > 详细

hibernate4笔记

时间:2017-02-09 18:46:50      阅读:149      评论:0      收藏:0      [点我收藏+]

HQL查询的helloworld

技术分享
@Test
public void testHQL(){
    
    //1. 创建 Query 对象
    //基于位置的参数. 
    String hql = "FROM Employee e WHERE e.salary > ? AND e.email LIKE ? AND e.dept = ? "
            + "ORDER BY e.salary";
    Query query = session.createQuery(hql);
    
    //2. 绑定参数
    //Query 对象调用 setXxx 方法支持方法链的编程风格.
    Department dept = new Department();
    dept.setId(80); 
    query.setFloat(0, 6000)
         .setString(1, "%A%")
         .setEntity(2, dept);
    
    //3. 执行查询
    List<Employee> emps = query.list();
    System.out.println(emps.size());  
}
testHQL
技术分享
@Test
public void testHQLNamedParameter(){
    
    //1. 创建 Query 对象
    //基于命名参数. 
    String hql = "FROM Employee e WHERE e.salary > :sal AND e.email LIKE :email";
    Query query = session.createQuery(hql);
    
    //2. 绑定参数
    query.setFloat("sal", 7000)
         .setString("email", "%A%");
    
    //3. 执行查询
    List<Employee> emps = query.list();
    System.out.println(emps.size());  
}
命名查询
技术分享
@Test
public void testPageQuery(){
    String hql = "FROM Employee";
    Query query = session.createQuery(hql);
    
    int pageNo = 22;
    int pageSize = 5;
    
    List<Employee> emps = 
                            query.setFirstResult((pageNo - 1) * pageSize)
                                 .setMaxResults(pageSize)
                                 .list();
    System.out.println(emps);
}
分页查询
技术分享
@Test
public void testFieldQuery(){
    String hql = "SELECT e.email, e.salary, e.dept FROM Employee e WHERE e.dept = :dept";
    Query query = session.createQuery(hql);
    
    Department dept = new Department();
    dept.setId(80);
    List<Object[]> result = query.setEntity("dept", dept)
                                 .list();
    
    for(Object [] objs: result){
        System.out.println(Arrays.asList(objs));
    }
}
投影查询
技术分享
@Test
public void testFieldQuery2(){
    String hql = "SELECT new Employee(e.email, e.salary, e.dept) "
            + "FROM Employee e "
            + "WHERE e.dept = :dept";
    Query query = session.createQuery(hql);
    
    Department dept = new Department();
    dept.setId(80);
    List<Employee> result = query.setEntity("dept", dept)
                                 .list();
    
    for(Employee emp: result){
        System.out.println(emp.getId() + ", " + emp.getEmail() 
                + ", " + emp.getSalary() + ", " + emp.getDept());
    }
}
投影查询2
技术分享
@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", 8000);
    
    List<Object []> result = query.list();
    for(Object [] objs: result){
        System.out.println(Arrays.asList(objs));
    }
}
报表查询

 

hibernate4笔记

原文:http://www.cnblogs.com/orco/p/6383185.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!