自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Hibernate之Criteria基本查詢

開發(fā) 后端
本文介紹Hibernate中的Criteria基本查詢。使用Hibernate時(shí),即使您不了解SQL的使用與撰寫,也可以使用它所提供的API來進(jìn)行SQL語句查詢,org.hibernate.Criteria對SQL進(jìn)行封裝。

要對資料庫管理系統(tǒng)進(jìn)行操作,最基本的就是使用SQL(Standard Query Language)語句,大部份的資料庫都支援標(biāo)準(zhǔn)的SQL語句,然而也有一些特定于資料庫的SQL語句,應(yīng)用程式配合SQL語句進(jìn)行資料庫查詢時(shí),若使用到特定于資料庫的SQL語句,程式本身會(huì)有相依于特定資料庫的問題。

使用Hibernate時(shí),即使您不了解SQL的使用與撰寫,也可以使用它所提供的API來進(jìn)行SQL語句查詢, org.hibernate.Criteria對SQL進(jìn)行封裝,您可以從Java物件的觀點(diǎn)來組合各種查詢條件,由Hibernate自動(dòng)為您產(chǎn)生 SQL語句,而不用特別管理SQL與資料庫相依的問題,就某個(gè)程度的意涵來看,這就像是在編譯時(shí)期也可以得到對SQL語法的檢查與驗(yàn)證。

以最基本的Criteria基本查詢來說,如果您想要查詢某個(gè)物件所對應(yīng)的資料表中所有的內(nèi)容,您可以如下進(jìn)行查詢:

  1. Criteria criteria = session.createCriteria(User.class);  
  2. List users = criteria.list();  
  3.           
  4. for(Iterator it = users.iterator(); it.hasNext(); ) {  
  5.     User user = (User) it.next();  
  6.     System.out.println(user.getId() +  
  7.                              " \t " + user.getName() +  
  8.                           "/" + user.getAge());      

Criteria建立后,若不給予任何的條件,預(yù)設(shè)是查詢物件所對應(yīng)表格之所有資料,如果您執(zhí)行以上的程式片段,并于設(shè)定檔中設(shè)定了了Hibernate的”show_sql”屬性,則可以在主控下看到以下的SQL語句之產(chǎn)生:

Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_

org.hibernate.Criteria實(shí)際上是個(gè)條件附加的容器,如果想要設(shè)定查詢條件,則要使用 org.hibernate.criterion.Restrictions的各種靜態(tài)方法傳回 org.hibernate.criterion.Criteria實(shí)例,傳回的每個(gè)org.hibernate.criterion.Criteria 實(shí)例代表著一個(gè)條件,您要使用org.hibernate.Criteria的add()方法加入這些條件實(shí)例,例如查詢” age”大于20且小于40的資料:

  1. Criteria criteria = session.createCriteria(User.class);  
  2. criteria.add(Restrictions.gt("age"new Integer(20)));  
  3. criteria.add(Restrictions.lt("age"new Integer(40)));  
  4. List users = criteria.list();  
  5.           
  6. for(Iterator it = users.iterator(); it.hasNext(); ) {  
  7.     User user = (User) it.next();  
  8.     System.out.println(user.getId() +  
  9.                               " \t " + user.getName() +  
  10.                              "/" + user.getAge());      

Restrictions的gt()方法表示大于(great than)的條件,而lt表示小于(less than)的條件,執(zhí)行以上程式片段,觀察所產(chǎn)生的SQL語句,將使用where與and子句產(chǎn)來完成SQL的條件查詢:

Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ where this_.age>? and this_.age< ?

使用add()方法加入條件時(shí),預(yù)設(shè)是使用and來組合條件,如果要用or的方式來組合條件,則可以使用Restrictions.or()方法,例如結(jié)合age等于(eq)20或(or)age為空(isNull)的條件:

  1. Criteria criteria = session.createCriteria(User.class);  
  2. criteria.add(Restrictions.or(  
  3.                    Restrictions.eq("age"new Integer(20)),  
  4.                    Restrictions.isNull("age")  
  5.                ));  
  6. List users = criteria.list(); 

觀察所產(chǎn)生的SQL語句,將使用where與or子句完成SQL的條件查詢:

Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ where (this_.age=? or this_.age is null)

您也可以使用Restrictions.like()方法來進(jìn)行SQL中l(wèi)ike子句的功能,例如查詢”name”中名稱為”just”開頭的資料:

  1. Criteria criteria = session.createCriteria(User.class);  
  2. criteria.add(Restrictions.like("name""just%"));  
  3. List users = criteria.list(); 

觀察所產(chǎn)生的SQL語句如下:

Hibernate: select this_.id as id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ where this_.name like ?

Restrictions的幾個(gè)常用限定查詢方法如下表所示:

方法 說明
Restrictions.eq 等于
Restrictions.allEq 使用Map,使用key/value進(jìn)行多個(gè)等于的比對
Restrictions.gt 大于 >
Restrictions.ge 大于等于 >=
Restrictions.lt 小于 <
Restrictions.le 小于等于 < =
Restrictions.between 對應(yīng)SQL的BETWEEN子句
Restrictions.like 對應(yīng)SQL的LIKE子句
Restrictions.in 對應(yīng)SQL的in子句
Restrictions.and and關(guān)系
Restrictions.or or關(guān)系
Restrictions.sqlRestriction SQL限定查詢

Criteria基本查詢差不多就是這樣。

【編輯推薦】

  1. Hibernate緩存簡介及分類
  2. Hibernate中g(shù)enerator屬性的意義
  3. hibernate Key Generator 主鍵生成方式
  4. Hibernate的主鍵生成機(jī)制
  5. Hibernate的事務(wù):事務(wù)對象的方法
責(zé)任編輯:book05 來源: 百度博客
相關(guān)推薦

2009-06-30 16:46:45

Criteria進(jìn)階查

2009-06-17 14:17:40

Criteria條件查Hibernate

2009-06-30 16:55:19

2009-09-28 12:57:54

Hibernate C

2009-09-24 13:03:38

Hibernate C

2009-06-26 16:15:04

Criteria的用法Hibernate

2009-06-18 10:07:03

CriteriaHibernate

2009-06-08 10:20:01

Hibernate查詢

2009-06-18 12:59:39

Criteria Qu深入淺出Hiberna

2009-06-30 16:57:21

Criteria查詢

2009-11-13 09:24:58

JPA 2.0Criteria AP

2009-06-12 15:13:12

Hibernate學(xué)習(xí)

2016-12-15 08:38:50

1 Hibernate基本配置

2009-06-17 08:47:00

Hibernate優(yōu)化

2009-09-24 15:27:41

Hibernate查詢

2009-06-18 09:14:08

Hibernate H

2009-06-26 10:01:00

Hibernate的查

2009-09-22 08:39:59

Hibernate F

2009-09-27 10:19:11

Hibernate命名

2009-09-29 16:29:40

Hibernate查詢
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)