Spring BeanUtils.copyProperties只拷贝不为null的属性

21 篇文章 0 订阅
13 篇文章 0 订阅

一:问题  BeanUtils.copyProperties拷贝属性不排除null

apache和spring的工具包中都有BeanUtils,使用其中的copyProperties方法可以非常方便的进行这些工作,但在实际应用中发现,对于null的处理不太符合个人的需要,例如在进行修改操作中只需要对model中某一项进行修改,那么一般我们在页面上只提交model的ID及需要修改项的值,这个时候使用BeanUtils.copyProperties会将其他的null绑定到pojo中去。

BeanUtils.copyProperties的使用要导入:

org.springframework.beans.BeanUtils;

import com.hourumiyue.system.SpringUtil;
import org.springframework.beans.BeanUtils;

public class TestBeanUtiles {

    public static void main(String[] args) {

        NewPerson newPerson = new NewPerson();
        newPerson.setName("miyue");//前台用户更新过的数据,例如前台只修改了用户名

        //下面我们假设是从数据库加载出来的老数据
        OldPerson oldPerson = new OldPerson();
        oldPerson.setSex("nv");
        oldPerson.setAge(5);
        //如果我们想把新数据更新到老数据这个对象里面,我们就可以借助BeanUtils.copyProperties()的方法如下:
        BeanUtils.copyProperties(newPerson, oldPerson);

     System.out.println(newPerson.toString());
        System.out.println(oldPerson.toString());
    }
}
运行结果:
NewPerson{name='miyue', sex='null', age=0}
OldPerson{name='miyue', sex='null', age=0}

从上面我们可以看出,新的对象确实把修改的数据更新给老对象了,但是它却把原来为null或者int类型默认为0的值也都赋给了老对象,这肯定不合理的。

二:解决问题 处理 BeanUtils.copyProperties排除null属性的copy

我们自己写了一个加工类,大家可以直接调用我们加工类的copyPropertiesIgnoreNull()方法即可忽略null值,避免老数据被null覆盖的尴尬:

import com.hourumiyue.system.SpringUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.HashSet;
import java.util.Set;

public class TestBeanUtiles {

	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));
    }
	
	public static void main(String[] args) {
        NewPerson newPerson = new NewPerson();
        newPerson.setName("miyue");//前台用户更新过的数据,例如前台只修改了用户名
        //下面我们假设是从数据库加载出来的老数据
        OldPerson oldPerson = new OldPerson();
        oldPerson.setSex("nv");
        oldPerson.setAge(5);
        //如果我们想把新数据更新到老数据这个对象里面,我们就可以借助BeanUtils.copyProperties()的方法如下:
        //BeanUtils.copyProperties(newPerson, oldPerson);
        copyPropertiesIgnoreNull(newPerson, oldPerson);
        System.out.println(newPerson.toString());
        System.out.println(oldPerson.toString());
    }
}

 

打印结果:
NewPerson{name='miyue', sex='null', age=0}
OldPerson{name='miyue', sex='nv', age=0}

 

 

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值