学习Common BeanUtils

根据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时。

 

在学习当中,发现了两位前辈的类似总结,写得很好。

http://www.blogjava.net/sean/articles/Jakarta_Commons_Notes.html

http://calvin.iteye.com/blog/92035

省了我不少时间,呵呵。

 

你可能感兴趣的:(apache,bean,.net,jdbc,Microsoft)