Beanutils (1)

2008-02-23 09:32:44来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

PropertyUtils类:负责对Bean的重新设置和更改,支持3种属性,SimpleIndexedMapped

Simple:

Employee employee = ...;
String firstName = (String)
PropertyUtils.getSimpleProperty(employee, "firstName");
String lastName = (String)
PropertyUtils.getSimpleProperty(employee, "lastName");
... manipulate the values ...
PropertyUtils.setSimpleProperty(employee, "firstName", firstName);
PropertyUtils.setSimpleProperty(employee, "lastName", lastName);

Indexed:

Employee employee = ...;
int index = ...;
String name = "subordinate[" index "]";
Employee subordinate = (Employee)
PropertyUtils.getIndexedProperty(employee, name);

Employee employee = ...;
int index = ...;
Employee subordinate = (Employee)
PropertyUtils.getIndexedProperty(employee, "subordinate", index);

Indexed支持2种写法,一种直接指定属性名以及Index,还有一种是把两这分别赋值,符号是方括号[]。

Mapped:

Employee employee = ...;
Address address = ...;
PropertyUtils.setMappedProperty(employee, "address(home)", address);

Employee employee = ...;
Address address = ...;
PropertyUtils.setMappedProperty(employee, "address", "home", address);

Mapped也支持2种写法,和Indexed不同的是他用的是Key,不是Index,符号是圆括号()。

同时它还支持属性的嵌套:

String city = employee.getAddress("home").getCity();


等价于

String city = (String)PropertyUtils.getNestedProperty(employee, "address(home).city");

以及以上情况的任意结合:

Employee employee = ...;
String city = (String) PropertyUtils.getProperty(employee,
"subordinate[3].address(home).city");

Dynamic Beans:最基本的接口是DynaBean和DynaClass。DynaBean负责生成动态的Bean,而DynaClass则是生成动态的Class。

BasicDynaBean和BasicDynaClass类

DynaProperty[] props = new DynaProperty[]{
new DynaProperty("address", Java.util.Map.class),
new DynaProperty("subordinate", mypackage.Employee[].class),
new DynaProperty("firstName", String.class),
new DynaProperty("lastName", String.class)
};
BasicDynaClass dynaClass = new BasicDynaClass("employee", null, props);

ResultSetDynaClass类

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();

但是ResultSetDynaClass是一定要在资源打开的情况下才能使用。

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

RowSetDynaClass 就不象ResultSetDynaClass,需要都打开,但是他占用更多的内存。

上一篇: To invoke Web Service in .htm file
下一篇: 用eclipse wtp 开发JSP

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:详解Java中的指针、引用及对象的clone

下一篇:JFreeChart 的用法