把Map的值设置进Bean : org.apache.commons.beanutils.BeanUtils.populate(Bean,Map);
bean To Map
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
/**
* bean转换为map
* @param obj
* @param dist
* @param ignoNull 是否忽略值为null
*/
public static void beanToMap(Object obj,Map<String,Object> dist,boolean ignoNull){
Map<String, Object> map = new HashMap<>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter != null ? getter.invoke(obj) : null;
if(ignoNull && value != null){ //去掉null
map.put(key, value);
}else{ //不去掉null
map.put(key, value);
}
}
dist.putAll(map);
} catch (Exception ex) {
log.error("系统异常", ex.getMessage());
}
}