常用的Hql语句

  1. // HQL: Hibernate Query Language.
    // 特点:
    // >> 1,与SQL相似,SQL中的语法基本上都可以直接使用。
    // >> 2,SQL查询的是表和表中的列;HQL查询的是对象与对象中的属性。
    // >> 3,HQL的关键字不区分大小写,类名与属性名是区分大小写的。
    // >> 4,SELECT可以省略.

    // 1,简单的查询,Employee为实体名而不是数据库中的表名(面向对象特性)
    hql = "FROM Employee";
    hql = "FROM Employee AS e"; // 使用别名
    hql = "FROM Employee e"; // 使用别名,as关键字可省略

    // 2,带上过滤条件的(可以使用别名):Where
    hql = "FROM Employee WHERE id<10";
    hql = "FROM Employee e WHERE e.id<10";
    hql = "FROM Employee e WHERE e.id<10 AND e.id>5";

    // 3,带上排序条件的:Order By
    hql = "FROM Employee e WHERE e.id<10 ORDER BY e.name";
    hql = "FROM Employee e WHERE e.id<10 ORDER BY e.name DESC";
    hql = "FROM Employee e WHERE e.id<10 ORDER BY e.name DESC, id ASC";

    // 4,指定select子句(不可以使用select *)
    hql = "SELECT e FROM Employee e"; // 相当于"FROM Employee e"
    hql = "SELECT e.name FROM Employee e"; // 只查询一个列,返回的集合的元素类型就是这个属性的类型
    hql = "SELECT e.id,e.name FROM Employee e"; // 查询多个列,返回的集合的元素类型是Object数组
    hql = "SELECT new Employee(e.id,e.name) FROM Employee e"; // 可以使用new语法,指定把查询出的部分属性封装到对象中

    // 5,执行查询,获得结果(list、uniqueResult、分页 )
    Query query = session.createQuery("FROM Employee e WHERE id<3");
    query.setFirstResult(0);
    query.setMaxResults(10); // 等同于 limit 0,10
    //两种查询结果list、uniqueResult
    // List list = query.list(); // 查询的结果是一个List集合
    // Employee employee = (Employee) query.uniqueResult();// 查询的结果是唯一的一个结果,当结果有多个,就会抛异常

    // 6,方法链
    List list = session.createQuery(//
    "FROM Employee e")//
    .setFirstResult(0)//
    .setMaxResults(10)//
    .list();

    // 7,聚集函数:count(), max(), min(), avg(), sum()
    hql = "SELECT COUNT(*) FROM Employee"; // 返回的结果是Long型的
    hql = "SELECT min(id) FROM Employee"; // 返回的结果是id属性的类型

    //8,分组: Group By ... Having
    hql = "SELECT e.name,COUNT(e.id) FROM Employee e GROUP BY e.name";
    hql = "SELECT e.name,COUNT(e.id) FROM Employee e GROUP BY e.name HAVING count(e.id)>1";
    hql = "SELECT e.name,COUNT(e.id) FROM Employee e WHERE id<9 GROUP BY e.name HAVING count(e.id)>1";
    hql = "SELECT e.name,COUNT(e.id) " + //
    "FROM Employee e " + //
    "WHERE id<9 " + //
    "GROUP BY e.name " + //
    "HAVING count(e.id)>1 " + //
    "ORDER BY count(e.id) ASC";
    hql = "SELECT e.name,COUNT(e.id) AS c " + //
    "FROM Employee e " + //
    "WHERE id<9 " + //
    "GROUP BY e.name " + //
    "HAVING count(e.id)>1 " + // 在having子句中不能使用列别名
    "ORDER BY c ASC"; // 在orderby子句中可以使用列别名

    // 9,连接查询 / HQL是面向对象的查询
    //>> 内连接(inner关键字可以省略)
    hql = "SELECT e.id,e.name,d.name FROM Employee e JOIN e.department d";
    hql = "SELECT e.id,e.name,d.name FROM Employee e INNER JOIN e.department d";
    //>> 左外连接(outer关键字可以省略)
    hql = "SELECT e.id,e.name,d.name FROM Employee e LEFT OUTER JOIN e.department d";
    //>> 右外连接(outer关键字可以省略)
    hql = "SELECT e.id,e.name,d.name FROM Employee e RIGHT JOIN e.department d";
    //可以使用更方便的方法
    hql = "SELECT e.id,e.name,e.department.name FROM Employee e";

    // 10,查询时使用参数
    // >> 方式一:使用'?'占位
    hql = "FROM Employee e WHERE id BETWEEN ? AND ?";
    List list2 = session.createQuery(hql)//
    .setParameter(0, 5)// 设置参数,第1个参数的索引为0。
    .setParameter(1, 15)//
    .list();

    // >> 方式二:使用变量名
    hql = "FROM Employee e WHERE id BETWEEN :idMin AND :idMax";
    List list3 = session.createQuery(hql)//
    .setParameter("idMax", 15)//
    .setParameter("idMin", 5)//
    .list();

    // 当参数是集合时,一定要使用setParameterList()设置参数值
    hql = "FROM Employee e WHERE id IN (:ids)";
    List list4 = session.createQuery(hql)//
    .setParameterList("ids", new Object[] { 1, 2, 3, 5, 8, 100 })//
    .list();

    // 11,update与delete,不会通知Session缓存
    // >> Update
    int result = session.createQuery(//
    "UPDATE Employee e SET e.name=? WHERE id>15")//
    .setParameter(0, "无名氏")//
    .executeUpdate(); // 返回int型的结果,表示影响了多少行。
    // >> Delete
    int result1 = session.createQuery(//
    "DELETE FROM Employee e WHERE id>15")//
    .executeUpdate(); // 返回int型的结果,表示影响了多少行。

  2. 1. 查询整个映射对象所有字段

    //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段  
            String hql = "from Users";  
            Query query = session.createQuery(hql);  
                
            List<Users> users = query.list();  
            for(Users user : users){  
                System.out.println(user.getName() + " : " + user.getPasswd() + " : " + user.getId());  
            }  
       
     输出结果为:  
    name1 : password1 : 1 
    name2 : password2 : 2 
    name3 : password3 : 3 

    2.查询字段  

    //查询其中几个字段  
            String hql = " select name,passwd from Users";  
            Query query = session.createQuery(hql);  
            //默认查询出来的list里存放的是一个Object数组  
            List<Object[]> list = query.list();  
            for(Object[] object : list){  
                String name = (String)object[0];  
                String passwd = (String)object[1];  
                    
                System.out.println(name + " : " + passwd);  
            }  
    输出结果为:  
    name1 : password1  
    name2 : password2  
    name3 : password3 

     

    3.修改默认查询结果(query.list())不以Object[]数组形式返回,以List形式返回

    //查询其中几个字段,添加new list(),注意list里的l是小写的。也不需要导入包,这样通过query.list()出来的list里存放的不再是默认的Object数组了,而是List集合了 
             String hql = " select new list(name,passwd) from Users";  
            Query query = session.createQuery(hql);  
            //默认查询出来的list里存放的是一个Object数组,但是在这里list里存放的不再是默认的Object数组了,而是List集合了  
            List<List> list = query.list();  
            for(List user : list){  
                String name = (String)user.get(0);  
                String passwd = (String)user.get(1);  
                    
                System.out.println(name + " : " + passwd);  
            }  
            /** 
            输出结果为: 
             name1 : password1 
            name2 : password2 
            name3 : password3 
             */ 

     

    4.修改默认查询结果(query.list())不以Object[]数组形式返回,以Map形式返回

    //查询其中几个字段,添加new map(),注意map里的m是小写的。也不需要导入包,这样通过query.list()出来的list里存放的不再是默认的Object数组了,而是map集合了  
            String hql = " select new map(name,passwd) from Users";  
            Query query = session.createQuery(hql);  
            //默认查询出来的list里存放的是一个Object数组,但是在这里list里存放的不再是默认的Object数组了,而是Map集合了  
            List<Map> list = query.list();  
            for(Map user : list){  
                //一条记录里所有的字段值都是map里的一个元素,key是字符串0,1,2,3....,value是字段值  
                //如果将hql改为:String hql = " select new map(name as username,passwd as password) from Users";,那么key将不是字符串0,1,2...了,而是"username","password"了  
                String name = (String)user.get("0");//get("0");是get(key),注意:0,1,2...是字符串,而不是整形  
                String passwd = (String)user.get("1");  
                    
                System.out.println(name + " : " + passwd);  
            }  
            /** 
            输出结果为: 
             name1 : password1 
            name2 : password2 
            name3 : password3 
             */ 

     

    5.修改默认查询结果(query.list())不以Object[]数组形式返回,以自定义类型返回

     

    6.条件查询

    //条件查询,参数索引值从0开始,索引位置。通过setString,setParameter设置参数  
            String hql = "from Users where name=? and passwd=?";  
            Query query = session.createQuery(hql);  
            //第1种方式  
    //      query.setString(0, "name1");  
    //      query.setString(1, "password1");  
            //第2种方式  
            query.setParameter(0, "name1",Hibernate.STRING);  
            query.setParameter(1, "password1",Hibernate.STRING);  
            List<Users> list = query.list();  
            for(Users users : list){  
                System.out.println(users.getId());  
            }  

     

    //条件查询,自定义索引名(参数名):username,:password.通过setString,setParameter设置参数  
            String hql = "from Users where name=:username and passwd=:password";  
            Query query = session.createQuery(hql);  
            //第1种方式  
    //      query.setString("username", "name1");  
    //      query.setString("password", "password1");  
            //第2种方式,第3个参数确定类型  
            query.setParameter("username", "name1",Hibernate.STRING);  
            query.setParameter("password", "password1",Hibernate.STRING);  
            List<Users> list = query.list();  
            for(Users users : list){  
                System.out.println(users.getId());  
            

      

    //条件查询,通过setProperties设置参数  
            String hql = "from Users where name=:username and passwd=:password";  
            Query query = session.createQuery(hql);  
            //MyUser类的2个属性必须和:username和:password对应  
            MyUser myUser = new MyUser("name1","password1");  
            query.setProperties(myUser);  
            List<Users> list = query.list();  
            for(Users users : list){  
                System.out.println(users.getId());  
            

      

    7.update 数据

               执行SQL语句(为什么要用SQL语句,我想是为了执行某些复杂的SQL语句吧) 

    String sql="update Table set field = 'test'"
    Session session = HibernateSessionFactory.getSession();
    session.createSQLQuery(sql).executeUpdate();
    ts.commit();

     

         

       执行HQL语句   

    String hql="update Table set field = 'test'"
    Session session = HiberanteSessionFactory.getSession();
    Transaction ts = session.beginTransaction();
    Query query = session.createQuery(hql);
    query.executeUpdate();
    ts.commit();
posted @ 2018-01-03 11:08  在途中#  阅读(19602)  评论(0编辑  收藏  举报