hibernate的hql查詢語句總結

這篇隨筆將會記錄hql的經常使用的查詢語句,爲往後查看提供便利。html

在這裏經過定義了三個類,Special、Classroom、Student來作測試,Special與Classroom是一對多,Classroom與Student是一對多的關係,這裏僅僅貼出這三個bean的屬性代碼:sql

Special類:數組

複製代碼

public class Special
{
    private int id;
    private String name;
    private String type;
    private Set<Classroom> rooms;
    ..........
}

複製代碼

Classroom類:session

複製代碼

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當中hibernate

2.基於 ? 的參數化形式ssr

複製代碼

       /**
             * 查詢中使用?,經過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開始的。htm

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();

基本上用到的hql查詢語句就是這些,之後若再遇到會進行補充。

轉自:http://www.cnblogs.com/xiaoluo501395377/p/3376256.html

相關文章
相關標籤/搜索