• Hibernate查询语言(HQL)


    转自:https://www.cnblogs.com/gx-java/p/5903230.html
    HQL是Hibernate Query Language即Hibernate查询语言
    1、执行HQL语句的步骤:
    (1)、获取Session对象
    Configuration cfg = new Configuration();
    SessionFactory sessionFactory = cfg.buildSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    (2)、编写HQL语句
    (3)、创建Query
    session.createQuery(hql);
     
    (4)、执行查询,得到查询结果。执行HQL语句
     
    2、在HQL语句中绑定参数
    方法一:按参数位置绑定
    Query q = HibernateSessionFactory.getSession().createQuery("from User u where u.name like =?");
    q.setParameter(0, "");
     
    方法二:按参数名字绑定
    Query q = HibernateSessionFactory.getSession().createQuery("from User u where u.name like :name");
    q.setParameter("name", "");
     
    setProperties(可以为对象和集合)
     
    3、动态查询
     
    String hql="from Users u where u.name=? and u.password=? ";
    session.createQuery(hql).setParameter(0, "小明").setParameter(1, "123456");
     
    public List<User> findUser2(Object[] obj) {
    Query q = HibernateSessionFactory.getSession().createQuery(
    "from User u where u.name like ?");
    for (int i = 0; i < obj.length; i++) {
    q.setParameter(i, obj[i]);
    }
    return q.list();
    }
     
    4、投影查询HQL投影查询是查询一个持久化类的一个或多个属性值
    将每条查询结果封装成Object对象
    String sql="select u.name from User u";
    将每条查询结果封装成Object数组
    String sql="select u.name,u.id from User u";
    将每条查询结果通过构造函数封装成对象
    String sql="select new login(u.name,u.passwoord) from User u";
     
    5、分页查询
    uniqueResult()方法获取唯一对象
    public long findCount(String hql) {
    Object o =(Object)session.createQuery(hql).uniqueResult();
    return Long.parseLong(o.toString());
    }
    setFirstResult()方法设置从第几条开始
    setFirstResult((page - 1) * size)
    setMaxResults()方法设置读取最大记录数
    .setMaxResults(size)
    public List<User> findByPage(String hql, Map<String, Object> user,
    int page, int size) {
    return HibernateSessionFactory.getSession()
    .createQuery(hql)
    .setProperties(user)
    .setFirstResult((page - 1) * size)
    .setMaxResults(size)
    .list();
    }
     
    // 获取总条数 select count (*) COUNT from ... order by ...
    public Long findCountOfUser(String hql, Map<String, Object> user) {
     
    return (Long) HibernateSessionFactory.getSession()
    .createQuery("select count (*) from User").setProperties(user)
    .uniqueResult();
    }
     
    6、hibernate HQL查询总结
     
    Special类:
    public class Special { private int id; private String name; private String type; private Set<Classroom> rooms; .......... }
    ---------------------------------------------
    Classroom类:
    public class Classroom { private int id; private String name; private Special special; private Set<Student> students;   ............ }
    ---------------------------------------------
    Student类:
    public class Student { private int id; private String name; private String sex; private Classroom room; .......... }
    ---------------------------------------------
    (1).最简单的查询
    List<Special> specials = (List<Special>)session.createQuery("select spe from Special spe").list();
    这是hql最基本的查询语句了,作用就是查出所有的Special对象放到一个List当中
     
    (2).基于 ? 的参数化形式
           /** * 查询中使用?,通过setParameter的方式可以防止sql注入 * jdbc的setParameter的下标从1开始,hql的下标从0开始 */ List<Student> students = (List<Student>)session.createQuery("select stu from Student stu where name like ?") .setParameter(0, "%刘%") .list();
     
    在hql中同样支持基于 ? 的参数化形式查询,注意:在jdbc中,setParameter的下标是从1开始的,而hibernate的setParameter的下标是从0开始的。
     
    (3).基于 :xx 的别名的方式设置参数
           /** * 在hql中可以使用别名的方式来查询,格式是 :xxx 通过setParameter来设置别名 */ List<Student> students = (List<Student>)session.createQuery("select stu from Student stu where name like :name and sex like :sex") .setParameter("name", "%王%").setParameter("sex", "%男%") .list();
     
    (4).如果返回的值只有一个,可以使用uniqueResult方法
           /** * 如果得到的值只有一个,则可以使用uniqueResult方法 */ Long stu = (Long)session.createQuery("select count(*) from Student stu where name like :name and sex like :sex") .setParameter("name", "%王%").setParameter("sex", "%男%") .uniqueResult();        /** * 如果得到的值只有一个,则可以使用uniqueResult方法 */ Student stu = (Student)session.createQuery("select stu from Student stu where id = ?") .setParameter(0, 1) .uniqueResult();
    (5).基于投影的查询
           /** * 基于投影的查询,如果返回多个值,这些值都是保存在一个object[]数组当中 */ List<Object[]> stus = (List<Object[]>)session.createQuery("select stu.name, stu.sex from Student stu where name like
                                :name and sex like :sex") .setParameter("name", "%张%").setParameter("sex", "%男%") .list();
    (6).基于导航对象的查询
           /** * 如果对象中有导航对象,可以直接通过对象导航查询 */ List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where stu.room.name like :room and sex like :sex") .setParameter("room", "%计算机应用%").setParameter("sex", "%女%") .list();
    注意:若直接通过导航对象来查询时,其实际是使用cross join(笛卡儿积)来进行连接查询,这样做性能很差,不建议使用
    (7).使用 in 进行列表查询
           /** * 可以使用in设置基于列表的查询,使用in查询时需要使用别名来进行参数设置, * 通过setParameterList方法即可设置,在使用别名和?的hql语句查询时,?形式的查询必须放在别名前面 */ // List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where sex like ? and stu.room.id in (:room)") // .setParameter(0, "%女%").setParameterList("room", new Integer[]{1, 2}) // .list(); List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where stu.room.id in (:room) and stu.sex like :sex") .setParameterList("room", new Integer[]{1, 2}).setParameter("sex", "%女%") .list();
    在使用 in 进行列表查询时,这个时候要通过 setParameterList() 方法来设置我们的参数,注意:如果一个参数通过别名来传入,一个是通过 ? 的方式来传入的话,那么通过别名的hql语句以及参数设置语句要放在 ? 的后面,不然hibernate会报错。如果都是使用 别名 来设置参数,则无先后顺序
     
    (8).分页查询
           /** * 通过setFirstResult(0).setMaxResults(10)可以设置分页查询,相当于offset和pagesize */ List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu where stu.room.name like :room and sex like :sex") .setParameter("room", "%计算机应用%").setParameter("sex", "%女%").setFirstResult(0).setMaxResults(10) .list();
    (9).内连接查询
           /** * 使用对象的导航查询可以完成连接查询,但是使用的是Cross Join(笛卡儿积),效率不高,所以建议使用join来查询 */ List<Student> stus = (List<Student>)session.createQuery("select stu from Student stu join stu.room room where room.id=2") .list();
    在hql中使用连接查询的语句与我们的sql进行连接查询的语句是有区别的:
    hql: select stu from Student stu join stu.room room sql: select t.* from Student t join Classroom c on t.cid=c.id
     
    (10).左外连和右外连查询
           /** * 左外连和右外连其实是相对的,left join 就是以左边的表为基准, right join 就是以右边的表为基准 */ List<Object[]> stus = (List<Object[]>)session.createQuery("select room.name, count(stu.room.id) from Student stu right join stu.room room group by room.id") .list();
    (11).创建DTO类,将查询出来的多个字段可以存放到DTO对象中去
           /** * 当如果我们查询出多个字段的话,通常会创建一个DTO对象,用来存储我们查询出来的数据,通过 new XXX() 这样的方式 * 前提是XXX这个类里面必须要有接受这些字段的构造方法才行,而且必须要使用类的全名 */ // List<Object[]> stus = (List<Object[]>)session.createQuery("select stu.id,stu.name,stu.sex,room.name,special.name from Student stu left join stu.room room left join room.special special") // .list(); // for(Object[] obj : stus) // { // System.out.println(obj[0] + ", " + obj[1] + ", " + obj[2] + ", " + obj[3] + ", " + obj[4]); // }
     
    List<StudentDTO> stus = (List<StudentDTO>)session.createQuery("select new com.xiaoluo.bean.StudentDTO(stu.id, stu.name, stu.sex, room.name, special.name) from Student stu left join stu.room room left join room.special special")
                                 .list();
    (12).group having字句
    /** * 在hql中不能通过给查询出来的字段设置别名,别名只能设置在from 后面 */ List<Object[]> stus = (List<Object[]>)session.createQuery("select special.name, count(stu.room.special.id) from Student stu right join stu.room.special special group by special.id having count(stu.room.special.id)>150") .list();  // 查询出人数大于150个人的专业
           
           //  查询出每个专业中男生与女生的个数
           List<Object[]> stus = (List<Object[]>)session.createQuery("select special.name, stu.sex, count(stu.room.special.id) from Student stu right join stu.room.special special group by special.id,stu.sex") .list();
  • 相关阅读:
    省市联动js
    二级联动angularjs
    imageloader图片基本加载
    Gson解析json数组
    简单的异步处理AsyncTask
    网络请求HttpURLConnection
    图片加载框架ImageLoader
    Xlistview
    Image-Universal-Loader
    cropper.js裁剪图片的使用
  • 原文地址:https://www.cnblogs.com/sharpest/p/7724789.html
Copyright © 2020-2023  润新知