1.用法
BeanUtils.copyProperties(“要转换的类”, “转换后的类”);
2.扩展:Spring 的BeanUtils.copyProperties在拷贝属性时忽略空值
代码
public class CommonUtils {
public static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
for (PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) {
emptyNames.add(pd.getName());
}
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
}
用法:
BeanUtils.copyProperties(quote, existQuote, CommonUtils.getNullPropertyNames(quote));