兩位前輩的總結,寫得很好。
http://www.blogjava.net/sean/articles/Jakarta_Commons_Notes.html
http://www.blogjava.net/sean/archive/2005/08/02/9015.html
據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)
發現通用式最方便,可以替代上面所有的方式(搞不懂為啥還要弄那麼多)。
舉例:
//簡單式
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.動態Beans
基本式:(需要先定義屬性然後才能使用,不推薦)
BasicDynaBean and BasicDynaClass
包裝ResultSet式:(必須打開數據庫連接可以使用,不推薦)
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.數據類型的轉換
重點推薦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時。
(澳門)炒家、用家 樓市拉鋸戰
13 年前
沒有留言:
張貼留言