展開全部
首先需要?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;
}