隨筆-26  評論-44  文章-20  trackbacks-0
          劉偉javabean視頻(下部)(第2,3部分為轉(zhuǎn)載):
          org.apache.commons.beanutils包的使用

          1
          BeanUtils.getProperty(bean,property);//從bean中取出屬性property的東東
          BeanUtils.setProperty(bean,property,object);
          BeanUtils.copyProperties(bean1,bean2);//將2的屬性賦給1
          PropertyUtils與BeanUtils區(qū)別:BeanUtils的getProperty方法返回類型為String,而PropertyUtils的返回類型為Object(保持原樣).


          2
          ResultSetDynaClass (Wraps ResultSet in DynaBeans)

            一個很普通的DynaBean 的USER CASE就是用它來包裝其他原始集合,這些集合不是以JAVABEAN的形式展示的。最常見的情況就是當你請求JDBC驅(qū)動查詢SQL語句返回java.sql.ResultSet類型的記錄的時候,BeanUtils提供了標準的機制來把每一行resultset轉(zhuǎn)變?yōu)橐粋€ DynaBean,參照下列:

          Connection conn = ...;
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery
              ("select account_id, name from customers");
            Iterator rows = (new ResultSetDynaClass(rs)).iterator();
            while (rows.hasNext()) {
              DynaBean row = (DynaBean) rows.next();
              System.out.println("Account number is " +
                                 row.get("account_id") +
                                 " and name is " + row.get("name"));
            }
            rs.close();
            stmt.close();

          RowSetDynaClass (Disconnected ResultSet as DynaBeans)

          盡管ResultSetDynaClass是一個用來展示sql查詢的很好的技術(shù)(當成DynaBean),但是最大的問題就是在MVC的結(jié)構(gòu)之中,我們需要離線的取出查詢的所有數(shù)據(jù),而ResultSetDynaClass必須保持和數(shù)據(jù)庫相連。

          RowSetDynaClass 展示了解決這個問題的不同方法。當你構(gòu)造這樣的實例,那些原始的數(shù)據(jù)被復制到一系列in-memory 的DynaBeans來代表這些結(jié)果。這個技術(shù)的優(yōu)勢是,理所當然,你可以立即關(guān)閉ResultSet(和他相連的Statement),這些操作都可以在你處理被返回的數(shù)據(jù)之前。缺點就是,你需要為復制數(shù)據(jù)所需要的性能和內(nèi)存買單,而且數(shù)據(jù)的大小還得讓堆內(nèi)存可以適合。在許多情況下(特別是WEB APPS),這種折衷是有益處的。

            額外的方便就是,RowSetDynaClass 被定義為java.io.Serializable的實現(xiàn),因此它可以被序列化和反序列化。因此RowSetDynaClass展示了一種十分便利的方法來傳輸SQL結(jié)果到遠程Java-based 客戶端應(yīng)用程序(比如APPLET).

           基本的RowSetDynaClass使用模式如下所示:

          Connection conn = ...;  // Acquire connection from pool
              Statement stmt = conn.createStatement();
              ResultSet rs = stmt.executeQuery("SELECT ...");
              RowSetDynaClass rsdc = new RowSetDynaClass(rs);
              rs.close();
              stmt.close();
              ...;                    // Return connection to pool
              List rows = rsdc.getRows();
              ...;                   // Process the rows as desired

          WrapDynaBean and WrapDynaClass


          3
          Lazy DynaBeans(LazyDynaBean, LazyDynaMap and LazyDynaClass)

            你鐘情于DynaBeans是因為有了它你不必對每個pojo來編碼成一個class文件。這樣能夠?qū)崿F(xiàn)的原因是因為lazy--延遲加載。是以下的一些特性使得DynaBeans可以lazy:

            Lazy property addition (lazy屬性添加)---lazy beans 使用 實現(xiàn)了MutableDynaClass 接口的DynaClass,DynaClass提供了添加和刪除屬性的能力。當set方法調(diào)用的時候,Lazy beans 利用這個特性來自動添加DynaClass中沒有的屬性
          Lazy List/Array growth(lazy list/array 增長)---如果一個索引化的屬性沒有足夠的容量來容納要設(shè)置的屬性,那么List or Array 將會自動增長。
          Lazy List/Array instantiation(Lazy List/Array實例化) ---如果一個索引化的屬性并不存在,那么他將會調(diào)用 DynaBean的indexed property getter/setter methods(比如 get(name, index) or set(name, index, value))返回一個List 或者一個Array實例。如果一個索引化的屬性沒有在DynaClass中被定義,那么他將會被自動添加而且生成一個默認的list實現(xiàn)的實例。
          Lazy Map instantiation-------if a mapped property doesn't exist then calling the DynaBean's mapped property getter/setter methods (i.e. get(name, key) or set(name, key, value)) results in a new Map being instantiated. If the mapped property has not been defined in the DynaClass then it is automatically added and a default Map implementation instantiated.
          Lazy Bean instantiation -------如果一個DynaClass 中的屬性被定義成DynaBean 或者普通的bean,但是這個屬性并不在DynaBean中存在,那么LazyDynaBean將會采用默認的empty constructor來實例化這個 bean
          LazyDynaBean

          標準lazy bean 的實現(xiàn)。默認和實現(xiàn)了MutableDynaClass接口的LazyDynaClass相關(guān)聯(lián)---盡管他可以和MutableDynaClass的任何實現(xiàn)一起使用。例子如下:

           DynaBean dynaBean = new LazyDynaBean();

              dynaBean.set("foo", "bar");                   // simple

              dynaBean.set("customer", "title", "Mr");      // mapped
              dynaBean.set("customer", "surname", "Smith"); // mapped

              dynaBean.set("address", 0, addressLine1);     // indexed
              dynaBean.set("address", 1, addressLine2);     // indexed
              dynaBean.set("address", 2, addressLine3);     // indexed

          LazyDynaMap

          light wieght (輕量級)DynaBean facade to a Map with all the usual lazy features。之所以是輕量級,是因為他沒有和一個包含所有屬性的DynaClass相關(guān)連。事實上,他親自實現(xiàn)了DynaClass。一個LazyDynaMap可以用來包裝一個存在的map,也可以自己去實例化一個Map實例

          例如:

          If you need a new Map then to use....

          DynaBean dynaBean = new LazyDynaMap(); // create DynaBean
          dynaBean.set("foo", "bar"); // simple
          dynaBean.set("customer", "title", "Mr"); // mapped
          dynaBean.set("address", 0, addressLine1); // indexed
          Map myMap = dynaBean.getMap() // retrieve the Map or to use with an existing Map ....

          Map myMap = .... // exisitng Map
          DynaBean dynaBean = new LazyDynaMap(myMap); // wrap Map in DynaBean
          dynaBean.set("foo", "bar");

          LazyDynaClass
          繼承BasicDynaClass并實現(xiàn)MutableDynaClass接口。

          Either create a LazyDynaClass first... MutableDynaClass dynaClass = new LazyDynaClass(); // create DynaClass dynaClass.add("amount", java.lang.Integer.class); // add property dynaClass.add("orders", OrderBean[].class); // add indexed property dynaClass.add("orders", java.util.TreeMapp.class); // add mapped property DynaBean dynaBean = new LazyDynaBean(dynaClass); // Create DynaBean with associated DynaClass

          or create a LazyDynaBean and get the DynaClass... DynaBean dynaBean = new LazyDynaBean(); // Create LazyDynaBean MutableDynaClass dynaClass = (MutableDynaClass)dynaBean.getDynaClass(); // get DynaClass dynaClass.add("amount", java.lang.Integer.class); // add property dynaClass.add("myBeans", myPackage.MyBean[].class); // add 'array' indexed property dynaClass.add("myMap", java.util.TreeMapp.class); // add mapped property

           注意:

          MutableDynaClass 有一種受限(Restricted)屬性。When the DynaClass is restricted ,no properties can be added or removed from the DynaClass. Neither the LazyDynaBean or LazyDynaMap will add properties automatically if the DynaClass is restricted.


          posted on 2008-06-08 10:33 開機 閱讀(1296) 評論(0)  編輯  收藏 所屬分類: jsp+javabean

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 罗城| 永寿县| 湛江市| 陆河县| 顺义区| 晋宁县| 五台县| 四会市| 曲松县| 普洱| 修水县| 墨竹工卡县| 二连浩特市| 武川县| 阿拉善左旗| 武平县| 沙河市| 宣城市| 新晃| 酉阳| 和硕县| 花垣县| 浮山县| 丘北县| 田林县| 当涂县| 高邑县| 巴中市| 常熟市| 堆龙德庆县| 武穴市| 马公市| 莱州市| 隆昌县| 高阳县| 万年县| 大渡口区| 黄骅市| 勃利县| 嵊州市| 濮阳市|