mapstructMap不起作用

zujrkrfu  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(1402)

我有两个类order和targetorder

class Order {
   Location location;
   Address address;
}
class Location{
   double lat;
   double lng;
}

class Address{
   .
   .
   .
}

目标顺序如下所示:

class TargetOrder{
   TargetAddress targetAddress;
}

class TargetAddress{
   double lat;
   double lng;
   .
   .
   .
}

现在我已经为address到targetaddress编写了mapstructMap;
比如说:

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface OrderMapper {

    @Mapping(...)
    @Mapping(...)
    TargetAddress mapAddressToTargetAddress(Address address);

    @Mapping(target = "targetAddress", source = "address")
    @Mapping(target = "targetAddress.lat", source = "location.lat")
    @Mapping(target = "targetAddress.lng", source = "location.lng")
    TargetOrder mapOrderToTargetOrder(Order order);

当我这么做的时候, mapOrderToTargetOrder 实现不使用现有的 mapAddressToTargetAddress 但如果我移除

@Mapping(target = "targetAddress.lat", source = "location.lat")
@Mapping(target = "targetAddress.lng", source = "location.lng")

然后mapper使用现有的 mapAddressToTargetAddress .
我想使用现有的MapMap,然后Map位置属性以及。我试过使用 @MappingTarget 它也不起作用。
有没有其他方法来实现这一点?

gblwokeq

gblwokeq1#

尝试使用mapstruct @Named 注解并指定附加的 qualifiedByName 中的参数 @Mapping 注解。

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface OrderMapper {

    @Named("mapAddressToTargetAddress")
    @Mapping(...)
    @Mapping(...)
    TargetAddress mapAddressToTargetAddress(Address address);

    @Mapping(target = "targetAddress", source = "address", qualifiedByName = "mapAddressToTargetAddress")
    @Mapping(target = "lat", source = "location.lat")
    @Mapping(target = "lng", source = "location.lng")
    TargetOrder mapOrderToTargetOrder(Order order);
}

相关问题