BeanUtils.copyProperties() 所有的空值不复制

第一种情况: 所有为空值的属性都不copy
直接上代码吧~

public class UpdateUtil {
    /**
     * 所有为空值的属性都不copy
     *
     * @param source
     * @param target
     */
    public static void copyNullProperties(Object source, Object target) {
        BeanUtils.copyProperties(source, target, getNullField(source));
    }

    /**
     * 获取属性中为空的字段
     *
     * @param target
     * @return
     */
    private static String[] getNullField(Object target) {
        BeanWrapper beanWrapper = new BeanWrapperImpl(target);
        PropertyDescriptor[] propertyDescriptors = beanWrapper.getPropertyDescriptors();
        Set<String> notNullFieldSet = new HashSet<>();
        if (propertyDescriptors.length > 0) {
            for (PropertyDescriptor p : propertyDescriptors) {
                String name = p.getName();
                Object value = beanWrapper.getPropertyValue(name);
                if (Objects.isNull(value)) {
                    notNullFieldSet.add(name);
                }
            }
        }
        String[] notNullField = new String[notNullFieldSet.size()];
        return notNullFieldSet.toArray(notNullField);
    }

    public static void main(String[] args) {
        TopMenuConfigEntity topMenuConfigEntity1 = new TopMenuConfigEntity();
        topMenuConfigEntity1.setWardCode("cat");
        topMenuConfigEntity1.setTitle("animal");

        TopMenuConfigEntity topMenuConfigEntity2 = new TopMenuConfigEntity();
        topMenuConfigEntity2.setWardCode("dog");

        UpdateUtil.copyNullProperties(topMenuConfigEntity2,topMenuConfigEntity1);

        System.out.println(topMenuConfigEntity1.getTitle());
    }
}

执行main 方法后,topMenuConfigEntity1的title还是为原来的“animal”值,没有被topMenuConfigEntity2 的空值覆盖。

第二种情况:
原对象的属性有值,复制时指定某些字段不复制
调BeanUtils的这个方法

public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {
		copyProperties(source, target, null, ignoreProperties);
	}
 public static void main(String[] args) {
        TopMenuConfigEntity topMenuConfigEntity1 = new TopMenuConfigEntity();
        topMenuConfigEntity1.setWardCode("cat");
        topMenuConfigEntity1.setTitle("animal");
        topMenuConfigEntity1.setCreateTime(new Date());

        TopMenuConfigEntity topMenuConfigEntity2 = new TopMenuConfigEntity();

        String[] ignoreArray = new String[]{"title","createTime"};
        BeanUtils.copyProperties(topMenuConfigEntity2,topMenuConfigEntity1,ignoreArray);
        System.out.println("title : "+topMenuConfigEntity2.getTitle() +";createTime :" + topMenuConfigEntity2.getCreateTime());

    }

topMenuConfigEntity2的title 和createTime为null,没有复制

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BeanUtils.copyProperties方法是Apache Commons BeanUtils库中的一个工具方法,用于将一个Java对象的属性复制到另一个Java对象中。默认情况下,该方法会将源对象的所有属性复制到目标对象中,包括属性。 如果你想要排除属性,可以使用自定义的属性拷贝器(PropertyUtilsBean)来实现。以下是一种可能的实现方式: 1. 创建一个自定义的属性拷贝器类,继承自PropertyUtilsBean类,并重写copyProperties方法。 ```java import org.apache.commons.beanutils.PropertyUtilsBean; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.beanutils.DynaBean; public class CustomPropertyUtils extends PropertyUtilsBean { @Override public void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(orig); for (PropertyDescriptor origDescriptor : origDescriptors) { String name = origDescriptor.getName(); if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) { Object value = PropertyUtils.getSimpleProperty(orig, name); if (value != null) { PropertyUtils.setSimpleProperty(dest, name, value); } } } } } ``` 2. 在你的代码中使用自定义的属性拷贝器类进行属性拷贝。 ```java CustomPropertyUtils customPropertyUtils = new CustomPropertyUtils(); customPropertyUtils.copyProperties(destObject, sourceObject); ``` 这样,只有源对象中非的属性才会被复制到目标对象中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值