利用Java反射机制基于 org.springframework.beans.BeanUtils的再封装!上代码!
public class BeanCopyUtil {
public static String[] getNullPropertyNames (Object source) throws IllegalAccessException {
List<String> res = new ArrayList<>();
Class<?> aClass = source.getClass();
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field: declaredFields) {
field.setAccessible(true);
Object value = field.get(source);
if(value == null){
res.add(field.getName());
}
}
return res.toArray(new String[0]);
}
public static void copyPropertyIgnoreNullVal(Object source,Object target) throws IllegalAccessException {
BeanUtils.copyProperties(source,target,getNullPropertyNames(source));
}
}