PropertyUtils类:负责对Bean的重新设置和更改,支持3种属性,Simple ,Indexed ,Mapped 。
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,需要都打开,但是他占用更多的内存。