目标:将两个对象中的非空值赋值给第三个对象,要求第三个对象拥有前面两个对象中的非空值
方法:使用modelMapper
代码:
//定义第一个对象
Person person = new Person(); person.setName("1");
//定义第二个对象 Person person1 = new Person(); person1.setHigh(BigDecimal.ONE);
//定义目标对象 Person person2 = new Person();
//定义modelMapper并将配置设置为null值不进行赋值操作 ModelMapper modelMapper = new ModelMapper(); modelMapper.getConfiguration() .setSkipNullEnabled(true); modelMapper.map(person,person2); modelMapper.map(person1,person2); System.out.println(person); System.out.println(person1); System.out.println(person2); //对象 @Data public class Person { private String name; private int age; private BigDecimal high; }
结果展示:
标签:java,--,person1,person2,Person,modelMapper,对象,new From: https://www.cnblogs.com/ahulwj/p/18291445