劉偉javabean視頻(下部)(第2,3部分為轉載):
org.apache.commons.beanutils包的使用
1
BeanUtils.getProperty(bean,property);//從bean中取出屬性property的東東
BeanUtils.setProperty(bean,property,object);
BeanUtils.copyProperties(bean1,bean2);//將2的屬性賦給1
PropertyUtils與BeanUtils區別:BeanUtils的getProperty方法返回類型為String,而PropertyUtils的返回類型為Object(保持原樣).
2
ResultSetDynaClass (Wraps ResultSet in DynaBeans)
一個很普通的DynaBean 的USER CASE就是用它來包裝其他原始集合,這些集合不是以JAVABEAN的形式展示的。最常見的情況就是當你請求JDBC驅動查詢SQL語句返回java.sql.ResultSet類型的記錄的時候,BeanUtils提供了標準的機制來把每一行resultset轉變為一個 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查詢的很好的技術(當成DynaBean),但是最大的問題就是在MVC的結構之中,我們需要離線的取出查詢的所有數據,而ResultSetDynaClass必須保持和數據庫相連。
RowSetDynaClass 展示了解決這個問題的不同方法。當你構造這樣的實例,那些原始的數據被復制到一系列in-memory 的DynaBeans來代表這些結果。這個技術的優勢是,理所當然,你可以立即關閉ResultSet(和他相連的Statement),這些操作都可以在你處理被返回的數據之前。缺點就是,你需要為復制數據所需要的性能和內存買單,而且數據的大小還得讓堆內存可以適合。在許多情況下(特別是WEB APPS),這種折衷是有益處的。
額外的方便就是,RowSetDynaClass 被定義為java.io.Serializable的實現,因此它可以被序列化和反序列化。因此RowSetDynaClass展示了一種十分便利的方法來傳輸SQL結果到遠程Java-based 客戶端應用程序(比如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文件。這樣能夠實現的原因是因為lazy--延遲加載。是以下的一些特性使得DynaBeans可以lazy:
Lazy property addition (lazy屬性添加)---lazy beans 使用 實現了MutableDynaClass 接口的DynaClass,DynaClass提供了添加和刪除屬性的能力。當set方法調用的時候,Lazy beans 利用這個特性來自動添加DynaClass中沒有的屬性
Lazy List/Array growth(lazy list/array 增長)---如果一個索引化的屬性沒有足夠的容量來容納要設置的屬性,那么List or Array 將會自動增長。
Lazy List/Array instantiation(Lazy List/Array實例化) ---如果一個索引化的屬性并不存在,那么他將會調用 DynaBean的indexed property getter/setter methods(比如 get(name, index) or set(name, index, value))返回一個List 或者一個Array實例。如果一個索引化的屬性沒有在DynaClass中被定義,那么他將會被自動添加而且生成一個默認的list實現的實例。
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 的實現。默認和實現了MutableDynaClass接口的LazyDynaClass相關聯---盡管他可以和MutableDynaClass的任何實現一起使用。例子如下:
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相關連。事實上,他親自實現了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并實現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
開機 閱讀(1286)
評論(0) 編輯 收藏 所屬分類:
jsp+javabean