11

我创建如下映射。如何将平面 dto 对象属性(如(街道、城市等))映射到域对象中的嵌套地址。当我尝试时出现错误:

[错误] 诊断:返回类型中的未知属性“address.postalCode”。@Mapping(source = "city", target = "address.city"),

@Mapper(componentModel = "spring", uses = {})
public interface CompanyMapper {
    @Mappings({
            @Mapping(source = "id", target = "id"),
            @Mapping(source = "street", target = "address.street"),
            @Mapping(source = "city", target = "address.city"),
            @Mapping(source = "postalCode", target = "address.postalCode"),
            @Mapping(source = "province", target = "address.province"),
    })
    DomainObject map(DtoObject dto);

还有课...

public class Address {
            private String street;
            private Integer streetNumber;
            private String city;
            private String postalCode;
            private String province;
            //getters and setters
    }
public class DomainObject {
        private String id;
        private Address address;
        //getters and setters
}

public class DtoObject {
        private String id;
        private String street;
        private String city;
        private String postalCode;
        private String province;
        //getters and setters
}
4

2 回答 2

9

尚不支持在您尝试使用它时嵌套在目标端。对此有一个功能请求(问题 #389),但我们还没有实现它。

于 2016-06-14T07:09:35.090 回答
2

我找不到一种方法来做到这一点。这是我的解决方案:

@Mapper
public interface DtoObjectMapper {

    Address toAddress(DtoObject dtoObject);

    DomainObject toDomainObject(DtoObject dtoObject, Address address);

}

使用时;

@Component
public class SomeClass {

    @Autowired
    private DtoObjectMapper dtoObjectMapper;

    public DomainObject convert(DtoObject dtoObject) {
        return dtoObjectMapper.toDomainObject(dtoObject, dtoObjectMapper.toAddress(dtoObject));
    }
}
于 2017-04-28T10:33:20.903 回答