[英]Map nested fields with MapStruct
假設我有這些實體:
public class Address {
private String id;
private String address;
private City city;
}
public class City {
private int id;
private Department department;
private String zipCode;
private String name;
private Double lat;
private Double lng;
}
public class Department {
private int id;
private Region region;
private String code;
private String name;
}
public class Region {
private int id;
private String code;
private String name;
}
而這個 DTO:
public class AddressDTO {
private String address;
private String department;
private String region;
private String zipCode;
}
在我的 DTO 中,我想 map
這是我的映射器:
@Mapper(componentModel = "spring")
public interface AddressMapper {
AddressDTO addressToAddressDTO(Address item);
}
當您映射嵌套字段時,您需要告訴 MapStruct 您希望從哪里以及如何使用@Mapping
進行映射。
在你的情況下,它看起來像:
@Mapper(componentModel = "spring")
public interface AddressMapper {
@Mapping(target = "department", source = "city.department.name")
@Mapping(target = "region", source = "city.department.region.name")
@Mapping(target = "zipCode", source = "city.zipCode")
AddressDTO addressToAddressDTO(Address item);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.