欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Hibernate中Criteria基本查詢的示例分析

這篇文章給大家分享的是有關Hibernate中Criteria基本查詢的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)是專業(yè)的鐘山網(wǎng)站建設公司,鐘山接單;提供成都做網(wǎng)站、網(wǎng)站設計,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行鐘山網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

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

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

以最基本的Criteria基本查詢來說,如果您想要查詢某個物件所對應的資料表中所有的內容,您可以如下進行查詢:

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建立后,若不給予任何的條件,預設是查詢物件所對應表格之所有資料,如果您執(zhí)行以上的程式片段,并于設定檔中設定了了Hibernate的”show_sql”屬性,則可以在主控下看到以下的SQL語句之產生:

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

org.hibernate.Criteria實際上是個條件附加的容器,如果想要設定查詢條件,則要使用 org.hibernate.criterion.Restrictions的各種靜態(tài)方法傳回 org.hibernate.criterion.Criteria實例,傳回的每個org.hibernate.criterion.Criteria 實例代表著一個條件,您要使用org.hibernate.Criteria的add()方法加入這些條件實例,例如查詢” 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í)行以上程式片段,觀察所產生的SQL語句,將使用where與and子句產來完成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()方法加入條件時,預設是使用and來組合條件,如果要用or的方式來組合條件,則可以使用Restrictions.or()方法,例如結合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();

觀察所產生的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()方法來進行SQL中l(wèi)ike子句的功能,例如查詢”name”中名稱為”just”開頭的資料:

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

觀察所產生的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的幾個常用限定查詢方法如下表所示:

方法說明
Restrictions.eq等于
Restrictions.allEq使用Map,使用key/value進行多個等于的比對
Restrictions.gt大于 >
Restrictions.ge大于等于 >=
Restrictions.lt小于 <
Restrictions.le小于等于 < =
Restrictions.between對應SQL的BETWEEN子句
Restrictions.like對應SQL的LIKE子句
Restrictions.in對應SQL的in子句
Restrictions.andand關系
Restrictions.oror關系
Restrictions.sqlRestrictionSQL限定查詢

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

感謝各位的閱讀!關于“Hibernate中Criteria基本查詢的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

分享名稱:Hibernate中Criteria基本查詢的示例分析
文章來源:http://chinadenli.net/article40/ppggho.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、定制網(wǎng)站、網(wǎng)站策劃、全網(wǎng)營銷推廣、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)