一:spring的工具類方法:BeanUtils.copyProperties(orderMasterDTO, orderMasterDO);spring
做用:將orderMasterDTO對象中的屬性值,賦值到orderMasterDO中,其主要目的是利用反射機制對JavaBean的屬性進行拷貝。app
/** * Copy the property values of the given source bean into the target bean. * <p>Note: The source and target classes do not have to match or even be derived * from each other, as long as the properties match. Any bean properties that the * source bean exposes but the target bean does not will silently be ignored. * <p>This is just a convenience method. For more complex transfer needs, * consider using a full BeanWrapper. * @param source the source bean * @param target the target bean * @throws BeansException if the copying failed * @see BeanWrapper */ public static void copyProperties(Object source, Object target) throws BeansException { copyProperties(source, target, null, (String[]) null); }
二:好處:ide
不使BeanUtils.copyProperties(orderMasterDTO, orderMasterDO)方法的話,傳統的作法是:手動將orderMasterDTO的屬性值set到orderMasterDO中工具
OrderMasterDO orderMasterDO = new OrderMasterDO(); orderMasterDO.setOrderId(orderMasterDTO.getOrderId()); orderMasterDO.setBuyerName(orderMasterDTO.getBuyerName()); orderMasterDO.setOrderStatus(orderMasterDTO.getOrderStatus()); orderMasterDO.setCreateTimestamp(orderMasterDTO.getCreateTimestamp()); orderMasterDO.setUpdateTimestamp(orderMasterDTO.getUpdateTimestamp());
而使用了BeanUtils的工具方法,只需BeanUtils.copyProperties(orderMasterDTO, orderMasterDO)就能夠ojbk,簡單方便多了。spa
注意:要注意該方法使用的地方,否則頗有可能出現屬性值丟失的問題code