rethink

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            11 隨筆 :: 0 文章 :: 16 評論 :: 0 Trackbacks
          根據Common BeanUtils的用戶指南學習了很多有用的工具類.

          參考:http://commons.apache.org/beanutils/apidocs/org/apache/commons/beanutils/package-summary.html#package_description


          1. 屬性的存取

          簡單式:

          PropertyUtils.getSimpleProperty(Object bean, String name)
          PropertyUtils.setSimpleProperty(Object bean, String name, Object value)


          索引式:
          PropertyUtils.getIndexedProperty(Object bean, String name)
          PropertyUtils.getIndexedProperty(Object bean, String name, int index)
          PropertyUtils.setIndexedProperty(Object bean, String name, Object value)
          PropertyUtils.setIndexedProperty(Object bean, String name, int index, Object value)

          Map式:

          PropertyUtils.getMappedProperty(Object bean, String name)
          PropertyUtils.getMappedProperty(Object bean, String name, String key)
          PropertyUtils.setMappedProperty(Object bean, String name, Object value)
          PropertyUtils.setMappedProperty(Object bean, String name, String key, Object value)


          嵌套式:
          PropertyUtils.getNestedProperty(Object bean, String name)
          PropertyUtils.setNestedProperty(Object bean, String name, Object value)


          通用式:
          PropertyUtils.getProperty(Object bean, String name)
          PropertyUtils.setProperty(Object bean, String name, Object value)


          發現通用式最方便,可以替代上面所有的方式(搞不懂為啥還要弄那么多)。

          舉例:
          //簡單式
          System.out.println(PropertyUtils.getProperty(employee1, "lastName"));

          //索引式
          System.out.println(PropertyUtils.getProperty(employee1,"addr[0].city"));

          //Map式
          PropertyUtils.setProperty(employee1, "telphone(tel)""test1");
          System.out.println(PropertyUtils.getProperty(employee1, 
          "telphone(tel)"));

          //嵌套式
          String address = (String) PropertyUtils.getProperty(employee1, "address.addr");
          System.out.println(address);



          2. 動態Beans

          基本式:(需要先定義屬性然后才能使用,不推薦)

          BasicDynaBean and BasicDynaClass


          包裝ResultSet式:(必須打開數據庫連接可以使用,不推薦)

          ResultSetDynaClass


          包裝RowSet式:(可以不用打開連接使用,推薦)

          RowSetDynaClass

          舉例:

                  
          try {
                      Class.forName(
          "com.microsoft.jdbc.sqlserver.SQLServerDriver");
                      Connection conn 
          = DriverManager.getConnection(
                                      
          "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=java",
                                      
          "sa""sa");
                      Statement stmt 
          = conn.createStatement();
                      ResultSet rs 
          = stmt.executeQuery("select code,name from role");
                      RowSetDynaClass rsdc 
          = new RowSetDynaClass(rs);
                      rs.close();
                      stmt.close();
                 
                      List rows 
          = rsdc.getRows();
                      
          for (Object object : rows) {
                          DynaBean row 
          = (DynaBean) object;
                          System.out.println(
          "Role code is " +
                                             row.get(
          "code"+
                                             
          " and name is " + row.get("name"));
                      }
                     
                  } 
          catch (ClassNotFoundException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (SQLException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }

          懶加載式:(方便實用,重點推薦)

          LazyDynaBean

          舉例:

          LazyDynaBean ldb 
          = new LazyDynaBean();
          ldb.set(
          "test1""tt");
          ldb.set(
          "test2"null);
          ldb.set(
          "test3"new Employee());
          System.out.println(ldb.get(
          "test1"));
          System.out.println(ldb.get(
          "test2"));//null
          System.out.println(ldb.get("test3"));//顯示Employee.toString()信息

           并且也具有LazyDynaMap的功能。


          3. 數據類型的轉換

          重點推薦BeanUtils.populate方法。

          舉例:

                  Address bean 
          = new Address();
                  HashMap map 
          = new HashMap();
                  map.put(
          "zipCode1""zipCode");
                  map.put(
          "addr"new Long(1234));
                  map.put(
          "city""");
                  map.put(
          "country""country");
                  System.out.println(bean);
                  
          try {
                      BeanUtils.populate(bean, map);
                  } 
          catch (IllegalAccessException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  } 
          catch (InvocationTargetException e) {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  System.out.println(bean);

           常利用在action填充vo時。


          在學習當中,發現了兩位前輩的類似總結,寫得很好。

          http://www.aygfsteel.com/sean/articles/Jakarta_Commons_Notes.html

          http://calvin.javaeye.com/blog/92035

          省了我不少時間,呵呵。



          posted on 2008-09-28 16:45 rethink 閱讀(2416) 評論(2)  編輯  收藏 所屬分類: java

          評論

          # re: 學習Common BeanUtils 2008-09-28 23:06 stone2083
          推薦使用cglib的beancopier替代beanutil做copy工作。  回復  更多評論
            

          # re: 學習Common BeanUtils 2008-10-03 09:57 隔葉黃鶯
          LazyDynaBean 在 Struts 1.3 開始也用,免去了聲明動態 Formbean 的屬性聲明。

          RowSetDynaClass 這東西還不錯。  回復  更多評論
            

          主站蜘蛛池模板: 邹城市| 淮滨县| 东安县| 保德县| 汶川县| 德令哈市| 阜新| 阳泉市| 友谊县| 尤溪县| 永川市| 靖远县| 九寨沟县| 延津县| 新余市| 千阳县| 常宁市| 新巴尔虎右旗| 湄潭县| 荣昌县| 阿拉善左旗| 三江| 利津县| 尼木县| 穆棱市| 天峨县| 孟连| 乐安县| 开远市| 乳源| 安泽县| 沈阳市| 诏安县| 綦江县| 万源市| 海南省| 蓬溪县| 普格县| 黄冈市| 昌邑市| 忻州市|