java复制对象不为null的值到另一个对象(元素相同)

1、引入jar包

<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.10</version>
        </dependency>

2、测试

public static void main(String[] args) {
        MwmReleaseVersion releaseVersion = new MwmReleaseVersion();
        releaseVersion.setType("pad");
        MwmReleaseVersion old = new MwmReleaseVersion();
        old.setId("111111111111111111");
        old.setType("phone");
        BeanUtil.copyProperties(releaseVersion,old, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        System.out.println(JSON.toJSONString(old));
    }

BeanUtil.copyProperties:将第一个参数的元素值赋值给第二个参数,setIgnoreNullValue(true)为忽略null值

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。