rethink

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            11 隨筆 :: 0 文章 :: 16 評論 :: 0 Trackbacks
          根據(jù)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)


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

          舉例:
          //簡單式
          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. 動態(tài)Beans

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

          BasicDynaBean and BasicDynaClass


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

          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. 數(shù)據(jù)類型的轉(zhuǎn)換

          重點推薦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時。


          在學習當中,發(fā)現(xiàn)了兩位前輩的類似總結(jié),寫得很好。

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

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

          省了我不少時間,呵呵。



          posted on 2008-09-28 16:45 rethink 閱讀(2417) 評論(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 開始也用,免去了聲明動態(tài) Formbean 的屬性聲明。

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

          主站蜘蛛池模板: 黔江区| 盘山县| 合江县| 安岳县| 淮北市| 怀来县| 大冶市| 府谷县| 遂昌县| 定安县| 苍溪县| 泾阳县| 蓝田县| 邢台县| 南宫市| 蛟河市| 海林市| 临汾市| 左云县| 砀山县| 孙吴县| 天津市| 定边县| 扎囊县| 冷水江市| 扬中市| 阳城县| 尼木县| 西峡县| 苗栗县| 炎陵县| 深州市| 永川市| 观塘区| 彰武县| 竹山县| 黔西县| 保山市| 鄂尔多斯市| 宜宾市| 嘉峪关市|