BeanUtils.copyProperties();null值覆盖原值
时间: 2024-05-10 08:12:55 浏览: 16
BeanUtils.copyProperties() 是Apache Commons BeanUtils库中提供的一个方法,可以将一个Java对象的属性值复制到另一个Java对象中。如果源对象和目标对象中存在相同的属性名,那么默认情况下会将源对象的属性值复制到目标对象的属性值中,这就是所谓的“覆盖”。
如果源对象的某个属性值为null,而目标对象的相应属性值不为null,则默认情况下BeanUtils.copyProperties()方法会将源对象的null值也复制到目标对象中,从而覆盖原有的非null值。如果想要避免这种情况,可以在调用BeanUtils.copyProperties()方法之前先使用BeanUtilsBean.getInstance().getConvertUtils().register(false, true, 0)来禁止覆盖null值。
相关问题
BeanUtils.copyProperties 如何不覆盖式更新
BeanUtils.copyProperties方法的作用是将一个JavaBean对象的属性值复制到另一个JavaBean对象中,这是一种常见的JavaBean对象属性值拷贝工具。在进行不覆盖式更新时,可以使用BeanUtils.copyProperties的另一个重载方法,该方法为BeanUtils.copyProperties(Object source, Object target, String... ignoreProperties)。其中,ignoreProperties参数为一个可变长度的字符串数组,用于指定不需要复制的属性名。如果目标对象中的属性名在该数组中存在,则不会覆盖原有属性值,否则会将源对象中的属性值复制到目标对象中。例如,我们可以这样使用BeanUtils.copyProperties方法进行不覆盖式更新:
```
String[] ignoreProperties = {"id", "name"}; //指定不需要复制的属性名
BeanUtils.copyProperties(sourceObject, targetObject, ignoreProperties);
```
以上代码中,源对象为sourceObject,目标对象为targetObject。ignoreProperties数组中指定了不需要复制的属性名,例如"id"和"name"属性,这些属性在目标对象中已经有值,不需要被覆盖。如果源对象中有其他属性需要复制到目标对象中,则可以正常进行复制操作。
BeanUtils.copyProperties忽略null值
可以使用BeanMapper或者在BeanUtils.copyProperties中传入一个自定义的属性过滤器来忽略null值。以下是两种方法的示例代码:
1. 使用BeanMapper忽略null值
```python
import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.core.Converter;
public class BeanMapper {
private static final Map<String, BeanCopier> BEAN_COPIERS = new ConcurrentHashMap<>();
public static void copyProperties(Object source, Object target) {
String key = source.getClass().toString() + target.getClass().toString();
BeanCopier copier = null;
if (!BEAN_COPIERS.containsKey(key)) {
copier = BeanCopier.create(source.getClass(), target.getClass(), true);
BEAN_COPIERS.put(key, copier);
} else {
copier = BEAN_COPIERS.get(key);
}
copier.copy(source, target, new Converter() {
@Override
public Object convert(Object value, Class target, Object context) {
if (value == null) {
if (target == String.class) {
return "";
} else if (target == Integer.class) {
return 0;
} else if (target == Long.class) {
return 0L;
} else if (target == Double.class) {
return 0.0;
} else if (target == Float.class) {
return 0.0f;
} else if (target == Boolean.class) {
return false;
} else {
return null;
}
} else {
return value;
}
}
});
}
}
```
使用示例:
```python
Source source = new Source();
source.setName("test");
source.setAge(18);
source.setAddress(null);
Target target = new Target();
BeanMapper.copyProperties(source, target);
System.out.println(target.getName()); // 输出:test
System.out.println(target.getAge()); // 输出:18
System.out.println(target.getAddress()); // 输出:null
```
2. 在BeanUtils.copyProperties中传入自定义属性过滤器忽略null值
```python
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyDescriptor;
import java.util.HashSet;
import java.util.Set;
public class CopyUtils {
public static void copyPropertiesIgnoreNull(Object source, Object target) {
BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
}
private static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<>();
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);
}
}
```
使用示例:
```python
Source source = new Source();
source.setName("test");
source.setAge(18);
source.setAddress(null);
Target target = new Target();
CopyUtils.copyPropertiesIgnoreNull(source, target);
System.out.println(target.getName()); // 输出:test
System.out.println(target.getAge()); // 输出:18
System.out.println(target.getAddress()); // 输出:null
```