我正在尝试使用mapstruct 1.2.0.CR2映射嵌套属性。 (示例地图 customer.address.houseNumber 到 userDTO.homeDTO.addressDTO.houseNo )。
期望:当customer.address为null时,我不想将addressDTO设置为null。由于addressDTO包含" countyname"和其他已经从其他不同来源设置的属性。
请告知我是否有可以设置的属性/设置,以便在源为null时将目标设置为null。
@Mapper( nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS )
public interface CustomerUserMapperNullCheck {
@Mapping(source="address", target="homeDTO.addressDTO" )
void mapCustomer(Customer customer, @MappingTarget UserDTO userDTO) ;
@Mapping(source="houseNumber", target="houseNo" )
void mapCustomerHouse(Address address, @MappingTarget AddressDTO addrDTO) ;
}
我最初尝试过单一映射,如下所示
@Mapping(target="homeDTO.addressDTO.houseNo", source="address.houseNumber")
abstract void mapCustomerHouse(Customer customer, @MappingTarget UserDTO userDTO) ;
然后尝试根据https://github.com/mapstruct/mapstruct/issues/649拆分映射。
两种方法均未产生预期结果/ 生成的方法代码
protected void customerToHomeDTO(Customer customer, HomeDTO mappingTarget) {
if ( customer == null ) {
return;
}
if ( customer.getAddress() != null ) {
if ( mappingTarget.getAddressDTO() == null ) {
mappingTarget.setAddressDTO( new AddressDTO() );
}
mapCustomerHouse( customer.getAddress(), mappingTarget.getAddressDTO() );
}
**else {
mappingTarget.setAddressDTO( null ); // I dont want to else where addressDTO is set to null.
}**
}
完整生成的代码在这里 https://github.com/mapstruct/mapstruct/issues/1306
由于
答案 0 :(得分:0)
@Mapper( nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE )