1. add maven
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
<scope>compile</scope>
</dependency>
2. 参数赋值取值
PropertyUtils.getSimpleProperty(Object,
String)
PropertyUtils.setSimpleProperty(Object,
String, Object)
PropertyUtils.getIndexedProperty(Object,
String)
PropertyUtils.getIndexedProperty(Object,
String, int)
PropertyUtils.setIndexedProperty(Object,
String, Object)
PropertyUtils.setIndexedProperty(Object,
String, int, Object)
PropertyUtils.getMappedProperty(Object,
String)
PropertyUtils.getMappedProperty(Object,
String, String)
PropertyUtils.setMappedProperty(Object,
String, Object)
PropertyUtils.setMappedProperty(Object,
String, String, Object)
3. 嵌套赋值取值
PropertyUtils.getNestedProperty(Object,
String)
PropertyUtils.setNestedProperty(Object,
String, Object)
4.动态bean
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);
DynaBean employee = dynaClass.newInstance(); employee.set("address", new HashMap()); employee.set("subordinate", new mypackage.Employee[0]); employee.set("firstName", "Fred"); employee.set("lastName", "Flintstone");
MyBean bean = ...; DynaBean wrapper = new WrapDynaBean(bean); String firstName = wrapper.get("firstName");
LazyDynaBean 动态添加properties
5.类型转换
HttpServletRequest request = ...; MyBean bean = ...; HashMap map = new HashMap(); Enumeration names = request.getParameterNames(); while (names.hasMoreElements()) { String name = (String) names.nextElement(); map.put(name, request.getParameterValues(name)); } BeanUtils.populate(bean, map);
BeanUtils.cloneBean(Object bean)
BeanUtils.copyProperties(Object dest, Object orig)
BeanUtils.copyProperty(Object bean, String name, Object value)
BeanUtils.setProperty(Object bean, String name, Object value)
BeanUtils.getProperty(Object bean, String name)
6. 集合1. 比较 BeanComparator
Apache Common Beanutil,布布扣,bubuko.com
原文:http://www.cnblogs.com/zeng200103/p/3779589.html