(HQL)hibernate查詢語言

來源:互聯網
上載者:User

HQL執行個體:

1. 查詢表中的所有記錄:from Category

2. 帶有where子句的條件查詢:from Category c where c.name > 'c5'

3. 結果根據某一欄位排序:from Category c order by c.name desc(desc表示降序排列,asc表示升序排列)

4. 去除重複記錄獲得單一記錄:select distinct c from Category c order by c.name desc

5. 帶有參數的查詢:from Category c where c.id > :min and c.id < :max。hql語句中’:min’ 表示的是參數,可以像jdbc中一樣,為參數賦值。在hql中可以這樣,這裡也運用了鏈式編程:

session.createQuery("from Category c where c.id > :min and c.id < :max")

.setInteger("min", 2)

.setInteger("max", 8);

6. 帶參數hql查詢的另外一種查詢:from Category c where c.id > ? and c.id < ?

7. hibernate分頁查詢 

Query q = session.createQuery("from Category c order by c.name desc");

q.setMaxResults(3);

q.setFirstResult(0);

其中setMaxResult()是設定每頁的最大顯示量,setFirstResult()是設定其實元素從哪裡開始,這裡0代表最後一條元素。

8. 多表串連查詢:select t.title, c.name from Topic t join t.category c

9. HQL函數:

a) Count():select count(*) from Msg m

b) Max()-min()-avg():select max(m.id), min(m.id), avg(m.id), sum(m.id) from Msg m

c) Between:from Msg m where m.id between 3 and 5

d) In:from Msg m where m.id in (3, 4, 5)

10. Is null;is not null:from Msg m where m.cont is not null

11. Is empty:from Topic t where t.msgs is empty

12. Like:from Topic t where t.title like '%5'。'%'匹配所有字元,'_'匹配單個字元。

13. 

一些功能函數,但是不重要瞭解即可:select lower(t.title)," +

 "upper(t.title)," +

 "trim(t.title)," +

 "concat(t.title, '***')," +

 "length(t.title)" +

     " from Topic t ")

Trim()是去掉首尾空格,返回字串的副本,concat()將字串欲查詢出的字串串連。

14. Abs()-sqrt()-mod():select abs(t.id)," + "sqrt(t.id)," + "mod(t.id,2)" + " from Topic t 

15. 擷取當前的時間:select current_date, current_time, current_timestamp, t.id from Topic t

16. Having子句:select t.title, count(*) from Topic t group by t.title having count(*) <= 1

17. Exist:from Topic t where not exists (select m.id from Msg m where m.topic.id=t.id)

需要注意的一點:in同樣可以實現exist的功能,但是exist的執行效率較高。

18. Update的用法:update Topic t set t.title = upper(t.title)

19. hql刪除的三種方式:

Hibernate的刪除方式:

/*方式一*/
String hql = "select p from Province as p where p.id=?";
Query query = session.createQuery(hql);
query.setString(0, id);
Province p = (Province)query.list().get(0);
session.delete(p);
/*方式二*/
String hql = "delete Province where id=?";
Query query = session.createQuery(hql);
query.setString(0, id);
int x = query.executeUpdate();
if(x>0){
  flag = true;
}
/*方式三*/
Province p = (Province)session.get(Province.class, id);
session.delete(p);

方式一相對比較笨重。

方式二中的Hql語句不要加as + 別名!

方式三是Hibernate內建的方法。

20. 查詢表中的某些欄位:

方法一:給這個類建立一個構造方法,傳進去你想要的參數,然後hql語句可以這樣寫:

select new Class(c.name, c.date, c.sex) from Class

方法二:用JDBC的sql語句:

Session.createSQLQuery(sql);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.