Bootstrap

关于BeanUtils.copyProperties是否能正常复制字段【详细版】

话不多说!先总结:

1、字段相同,类型不同(不复制,也不报错)
2、子类父类
(1)子类传给父类(可以正常复制)
(2)父类传给子类(可以正常复制)
3、子类父类(属性类对象)
(1)子类属性传给父类属性(可以正常复制)
(2)父类属性传给子类属性(不复制,也不报错)

具体代码如下:
1、字段相同,类型不同(不复制,也不报错)

		RequestVO order = new RequestVO();
		order.setLinkId("linkId");
		order.setPayType("1");
		
		RequestVO2 order1 = new RequestVO2();
		BeanUtils.copyProperties(order, order1);
		
		System.out.println(order1.getLinkId());
		System.out.println(order1.getPayType());
linkId
null

2、子类父类
(1)子类传给父类(可以正常复制)

		RequestVTest order1 = new RequestVTest();
		order1.setLinkId("linkId1");
		order1.setOrderId("orderId1");
		order1.setPayType("payType");
		
		RequestVO order = new RequestVO();
		BeanUtils.copyProperties(order1, order);
		
		System.out.println(order.getLinkId());
		System.out.println(order.getPayType());
linkId1
payType

(2)父类传给子类(可以正常复制)

		OneToManyOrderRequestVTest order1 = new RequestVTest();
		
		RequestVO order = new RequestVO();
		order.setLinkId("linkId1");
		order.setOrderId("orderId1");
		BeanUtils.copyProperties(order, order1);
		
		System.out.println(order1.getLinkId());
		System.out.println(order1.getOrderId());
linkId1
orderId1

3、子类父类(属性类对象)
(1)子类属性传给父类属性(可以正常复制)

		UserRequestVO vo = new UserRequestVO();
		UserRequestVO2 vo1 = new UserRequestVO2();
		RequestVTest order1 = new RequestVTest();
		order1.setLinkId("linkId1");
		order1.setOrderId("orderId1");
		vo1.setOrderRequestVO(order1);
		
		BeanUtils.copyProperties(vo1, vo);
		System.out.println(vo.getOrderRequestVO().getLinkId());
		System.out.println(vo.getOrderRequestVO().getOrderId());
linkId1
orderId1

(2)父类属性传给子类属性(不复制,也不报错)

		UserRequestVO vo = new UserRequestVO();
		OrderRequestVO order1 = new OrderRequestVO();
		order1.setLinkId("linkId1");
		order1.setOrderId("orderId1");
		vo.setOrderRequestVO(order1);
		
		UserRequestVO2 vo1 = new UserRequestVO2();
		vo1.setOrderRequestVO(new OrderRequestVTest());
		BeanUtils.copyProperties(vo, vo1);
		System.out.println(vo1.getOrderRequestVO().getLinkId());
		System.out.println(vo1.getOrderRequestVO().getOrderId());
null
null
;