posts - 3,  comments - 2,  trackbacks - 0
          要對(duì)資料庫管理系統(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對(duì)SQL進(jìn)行封裝,您可以從Java物件的觀點(diǎn)來組合各種查詢條件,由Hibernate自動(dòng)為您產(chǎn)生 SQL語句,而不用特別管理SQL與資料庫相依的問題,就某個(gè)程度的意涵來看,這就像是在編譯時(shí)期也可以得到對(duì)SQL語法的檢查與驗(yàn)證。

          以最基本的查詢來說,如果您想要查詢某個(gè)物件所對(duì)應(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è)是查詢物件所對(duì)應(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è)等于的比對(duì)
          Restrictions.gt 大于 >
          Restrictions.ge 大于等于 >=
          Restrictions.lt 小于 <
          Restrictions.le 小于等于 <=
          Restrictions.between 對(duì)應(yīng)SQL的BETWEEN子句
          Restrictions.like 對(duì)應(yīng)SQL的LIKE子句
          Restrictions.in 對(duì)應(yīng)SQL的in子句
          Restrictions.and and關(guān)系
          Restrictions.or or關(guān)系
          Restrictions.sqlRestriction SQL限定查詢
          posted on 2009-06-25 11:51 sw0rd 閱讀(458) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 长宁区| 宿迁市| 阜阳市| 民丰县| 库尔勒市| 梁平县| 体育| 淮滨县| 晴隆县| 鄂尔多斯市| 乌恰县| 大安市| 乌海市| 绥芬河市| 涟源市| 贵州省| 台州市| 五原县| 赫章县| 阜阳市| 望都县| 凤庆县| 建宁县| 和平区| 商河县| 合水县| 绍兴县| 花莲县| 怀远县| 邻水| 本溪| 开化县| 新野县| 惠来县| 馆陶县| 沙河市| 彰化县| 昌吉市| 伽师县| 大邑县| 花莲县|