@Data public class ApplyTest extends BaseDTO { private String name; private String age; private String address; }
@Data public class InsurantTest extends BaseDTO { private String name; private String sex; }
import org.springframework.beans.BeanUtils;
ApplyTest applyTest = new ApplyTest(); applyTest.setName("张三"); applyTest.setAge("12"); applyTest.setAddress("湖北"); InsurantTest insurantTest = new InsurantTest(); BeanUtils.copyProperties(insurantTest,applyTest); System.out.println("=========result:"+insurantTest.toString());
结果是:
ApplyTest(name=null, age=12, address=湖北)
InsurantTest(name=null, sex=null)
总结:这个方法是将BeanUtils.copyProperties(a,b),a 中与b中相同字段赋值,如上图,a中所有字段值均为null,将a赋值给b,由于a和b共同的字段只有name,所以会将a中name值为null赋值给b.