[英]Nested Mapping using MapStruct
class Identifier {
private long id;
private String type;
private List<Status> statuses;
}
class Customer {
private Identifier identifier;
}
class CustomerProfile {
private Customer customer;
}
class CustomerIdentifierDO {
private long id;
}
class CustomeDO {
private CustomerIdentiferDO custID;
}
class CustomerProfileDO {
private String category;
private List<Status> custStatuses;
private CustomeDO customer;
}
@Mapper
public interface CustomerProfileMapper {
CustomerProfile toCustomerProfile(CustomerProfileDO profileDO) ;
Customer toCustomer(CustomerDO customerDO);
Identifier toIdentifier(CustomerIdentifierDO identifierDO);
}
Everything works fine till this.一切正常,直到这。 Now I want to map
custStatuses
, category
of CustomerProfileDO
class to statuses
and type
of Identifier
class.现在我想将
custStatuses
、 CustomerProfileDO
类的category
映射到Identifier
类的statuses
和type
。 I've no idea how to supply CustomerProfileDO
object to toIdentifier
mapping method, so that I can include the mapping there itself.我不知道如何将
CustomerProfileDO
对象提供给toIdentifier
映射方法,以便我可以在其中包含映射本身。 I tried following我试过
@Mappings({
@Mapping(target = "customer.identifier.type", source = "category")
})
CustomerProfile toCustomerProfile(CustomerProfileDO profileDO) ;
But this nested mapping is overriding all the mapping config of below method.但是这个嵌套映射覆盖了下面方法的所有映射配置。 That should not happen.
那不应该发生。
toIdentifer(CustomerIdentifierDO identifierDO)
Is there any way to achieve this?有没有办法实现这一目标?
Currently MapStruct can pass source parameters to single methods.目前 MapStruct 可以将源参数传递给单个方法。 In order to achieve what you are looking for (without using nested target types you would need to use something like
@AfterMapping
. It can look like:为了实现您正在寻找的东西(不使用嵌套目标类型,您需要使用类似
@AfterMapping
东西。它看起来像:
@Mapper
public interface CustomerProfileMapper {
CustomerProfile toCustomerProfile(CustomerProfileDO profileDO) ;
Customer toCustomer(CustomerDO customerDO);
Identifier toIdentifier(CustomerIdentifierDO identifierDO);
@AfterMapping
default void afterMapping(@MappingTarget CustomerProfile profile, CustomerProfieDO profileDO) {
Identifier identifier = profile.getCustomer().getIdentifier();
identifier.setStatus(profileDO.setStatus());
identifier.setType(profileDO.setCategory());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.