java vo转map_javabean实体类对象转为Map类型对象的方法(转发)

//将javabean实体类转为map类型,然后返回一个map类型的值

public static Map beanToMap(Object obj) {

Map params = new HashMap(0);

try {

PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();

PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(obj);

for (int i = 0; i < descriptors.length; i++) {

String name = descriptors[i].getName();

if (!"class".equals(name)) {

params.put(name, propertyUtilsBean.getNestedProperty(obj, name));

}

}

} catch (Exception e) {

e.printStackTrace();

}

return params;

}

放入上面方法的类需要 import java.beans.PropertyDescriptor;

需要引用两个jar包:commons-beanutils-1.9.2.jar   commons-logging-1.2.jar

获取上面两个jar的网址分别是:http://commons.apache.org/beanutils/   http://commons.ap

你可能感兴趣的:(java,vo转map)