使用 mapstruct 从单个元素映射嵌入式列表
mapping Embedded List from single element with mapstruct
我使用过 mappstruct,我遇到了一个问题,我不知道 mapstruct 是否可以帮助解决这个问题
问题是我有一个数据 class
public class Data {
private String firstName;
private String lastName;
private String state;
private String city;
private String street;
private String zipCode;
}
和地址
Entity
public class Address {
@Id
private Long id;
private String state;
private String city;
private String street;
private String zipCode;
和人
@Data
public class Person {
@Id
private Long id;
private String firstName;
private String lastName;
private String email;
private List<Address> list;
}```
So the person have a list of address and I want from One Data object create one adresse Object
and add that object to Person
So after the mapping I need to have Person with List(size == 1) of address
the mapper
@Mapper
public 接口 DataToPerson {
DataToPerson MAPPER = Mappers.getMapper(DataToPerson.class);
Person dataToPerson(Data data);
}
is that possible ?
Thank you so much
为了执行这样的映射,您需要告诉 MapStruct 如何映射到 List<Address>
。
例如
@Mapper
public interface DataToPerson {
DataToPerson MAPPER = Mappers.getMapper(DataToPerson.class);
@Mapping(target = "list", source = "data")
Person dataToPerson(Data data);
default List<Address> dataToAddressList(Data data) {
return data != null ? Collections.singletonList(dataToAddress(data)) : null;
}
Address dataToAddress(Data data);
}
我使用过 mappstruct,我遇到了一个问题,我不知道 mapstruct 是否可以帮助解决这个问题 问题是我有一个数据 class
public class Data {
private String firstName;
private String lastName;
private String state;
private String city;
private String street;
private String zipCode;
}
和地址
Entity
public class Address {
@Id
private Long id;
private String state;
private String city;
private String street;
private String zipCode;
和人
@Data
public class Person {
@Id
private Long id;
private String firstName;
private String lastName;
private String email;
private List<Address> list;
}```
So the person have a list of address and I want from One Data object create one adresse Object
and add that object to Person
So after the mapping I need to have Person with List(size == 1) of address
the mapper
@Mapper public 接口 DataToPerson {
DataToPerson MAPPER = Mappers.getMapper(DataToPerson.class);
Person dataToPerson(Data data);
}
is that possible ?
Thank you so much
为了执行这样的映射,您需要告诉 MapStruct 如何映射到 List<Address>
。
例如
@Mapper
public interface DataToPerson {
DataToPerson MAPPER = Mappers.getMapper(DataToPerson.class);
@Mapping(target = "list", source = "data")
Person dataToPerson(Data data);
default List<Address> dataToAddressList(Data data) {
return data != null ? Collections.singletonList(dataToAddress(data)) : null;
}
Address dataToAddress(Data data);
}