注意:只有实体属性的字段名和类型一样,才能赋值,最简单的就是实体A和实体B就class名不一样,其他一样;
import org.springframework.beans.BeanUtils;
public static String[] getNullPropertyNames (Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
for(java.beans.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);
}
public static void copyPropertiesIgnoreNull(Object src, Object target){
BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
}
BeanUtils.copyProperties(txzcaradd, txzcaraddhis, getNullPropertyNames(txzcaradd));null这里表示为null的时候不赋值