將一個實體類的值賦值給另一個實體類
這兩天做項目,用到了BeanUtils.copyProperties()這個方法,而在兩個不同的類中使用到這個方法,但不知怎么的,就是有一個對象服務發copy另外個對象,最后排查終於找到原因。
因為兩個類引入了兩個不同的BeanUtils類,
一個為org.springframework.beans.BeanUtils,
另一個是org.apache.commons.beanutils.BeanUtils,
這兩個類在不同的包下面,而這兩個類的copyProperties()方法里面傳遞的參數賦值是相反的。
例如:
a,b為對象
BeanUtils.copyProperties(a, b);
BeanUtils是org.springframework.beans.BeanUtils, a拷貝到b
BeanUtils是org.apache.commons.beanutils.BeanUtils,b拷貝到a
場景:
實體類 OrderInfo 賦值給 OrderVo.
關系:
OrderVo extends OrderInfo.
BeanUtils.copyProperties(OrderInfo,OrderVo);