目录
1. 获取对象属性值为空的属性名称
/**
* @Title: getNullPropertyNames
* @Description: 获取值为空的属性名称
* @createdBy:byrc
*/
public static String[] getNullPropertyNames(Object source) {
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);
}
2. 复制对象非空属性值
public static void main(String[] args){
User u1 = new User();
u1.setName("AAA");
u2.setArea("中国东莞");
User u2 = new User();
u2.setName("BB");
BeanUtils.copyProperties(u2, u1,getNullPropertyNames(u2));
}