隨筆-57  評論-202  文章-17  trackbacks-0
                Criteria Query是很好的一種面向對象的查詢實現,它提供了一種示例查詢的方式。該方式根據已有的對象,查找數據庫中屬性匹配的其他對象。
                下面是一個場景片斷,模糊查找數據庫中用戶帳號為'test',郵件地址為'georgehill@21cn.com'的實例,忽略大小寫。

            public void testCriteriaExampleQuery() throws Exception {
              User user 
          = new User();
              user.setAccount(
          "test");
              user.setEmail(
          "georgehill@21cn.com");
              
              Criteria criteria 
          = session.createCriteria(User.class).add(Example.create(user).enableLike(MatchMode.ANYWHERE).ignoreCase());
              List list 
          = criteria.list();
              
              
          if (list != null{
                
          for (int i = 0; i < list.size(); i++{
                  System.
          out.println(((User) list.get(i)).getAccount());
                }

              }

            }

                示例查詢需要生成Example實例,可以通過Example的靜態方法create生成。Example類有下面的幾個方法指定查詢的方式:

          excludeZeroes

          public Example excludeZeroes()
          Exclude zero-valued properties


          excludeNone

          public Example excludeNone()
          Don't exclude null or zero-valued properties


          enableLike

          public Example enableLike(MatchMode matchMode)
          Use the "like" operator for all string-valued properties


          enableLike

          public Example enableLike()
          Use the "like" operator for all string-valued properties


          ignoreCase

          public Example ignoreCase()
          Ignore case for all string-valued properties


          excludeProperty

          public Example excludeProperty(String name)
          Exclude a particular named property



                當用enableLike()方法時,可以通過MatchMode指定匹配的方式。MatchMode提供了四種匹配的方式:

          Field Summary
          static MatchMode ANYWHERE
                    Match the pattern anywhere in the string
          static MatchMode END
                    Match the end of the string to the pattern
          static MatchMode EXACT
                    Match the entire string to the pattern
          static MatchMode START
                    Match the start of the string to the pattern
          posted on 2005-06-03 17:27 小米 閱讀(2192) 評論(3)  編輯  收藏 所屬分類: Hibernate

          評論:
          # re: Criteria的示例查詢 2006-03-24 19:01 |
          很好,謝謝  回復  更多評論
            
          # re: Criteria的示例查詢 2007-02-11 09:26 | 高溥
          假如在表里有一個日期類型的屬性,想要查詢某個時間段的數據,該怎么辦?還是示例查詢  回復  更多評論
            
          # re: Criteria的示例查詢 2007-05-17 00:17 | yellowhat
          嗯 不錯!`  回復  更多評論
            
          主站蜘蛛池模板: 张家界市| 浦县| 肥乡县| 乾安县| 泰州市| 烟台市| 石首市| 房山区| 扶绥县| 安宁市| 榆社县| 海南省| 玉门市| 土默特右旗| 麦盖提县| 普陀区| 左贡县| 永年县| 阜南县| 寿宁县| 抚顺市| 定州市| 玉田县| 台江县| 永州市| 古浪县| 彭山县| 灵宝市| 定襄县| 隆化县| 突泉县| 姚安县| 桑植县| 昌邑市| 巴林左旗| 邵阳县| 东港市| 曲阜市| 循化| 县级市| 博客|