展开全部
首先需要 commons-beanutils jar包,然后转bean的方法为:62616964757a686964616fe59b9ee7ad9431333363386133/**
*
* @Title: transMap2Bean
* @param:@param map
* @param:@param obj
* @return:void
* @Description:Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean
* @throws
*/
public static void transMap2Bean(Map map, Object obj) {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (map.containsKey(key)) {
Object value = map.get(key);
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
setter.invoke(obj, value);
}
}
} catch (Exception e) {
System.out.println("transMap2Bean Error " + e);
}
return;
}