29 lines
802 B
Java
29 lines
802 B
Java
package com.base.util;
|
|
|
|
import java.beans.PropertyDescriptor;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
import org.apache.commons.beanutils.PropertyUtilsBean;
|
|
|
|
public class Bean2MapUtil {
|
|
public static Map<String, Object> beanToMap(Object obj) {
|
|
Map<String, Object> params = new HashMap<String, Object>(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;
|
|
}
|
|
}
|