Hibernate之Criteria基本查詢
要對資料庫管理系統(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)行查詢:
- Criteria criteria = session.createCriteria(User.class);
- List users = criteria.list();
- for(Iterator it = users.iterator(); it.hasNext(); ) {
- User user = (User) it.next();
- System.out.println(user.getId() +
- " \t " + user.getName() +
- "/" + 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的資料:
- Criteria criteria = session.createCriteria(User.class);
- criteria.add(Restrictions.gt("age", new Integer(20)));
- criteria.add(Restrictions.lt("age", new Integer(40)));
- List users = criteria.list();
- for(Iterator it = users.iterator(); it.hasNext(); ) {
- User user = (User) it.next();
- System.out.println(user.getId() +
- " \t " + user.getName() +
- "/" + 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)的條件:
- Criteria criteria = session.createCriteria(User.class);
- criteria.add(Restrictions.or(
- Restrictions.eq("age", new Integer(20)),
- Restrictions.isNull("age")
- ));
- 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”開頭的資料:
- Criteria criteria = session.createCriteria(User.class);
- criteria.add(Restrictions.like("name", "just%"));
- 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基本查詢差不多就是這樣。
【編輯推薦】