将列表映射到另一个并修改字段[Mapstruct]



我正在尝试使用mapstruct将一个列表映射到另一个列表。在映射时,我想在一个特定字段中添加一些信息,但无法做到。如果有人能提供一些解决方案,我会很高兴。

ex- If i've  List of People( {"1", "John"}, {"2", "Tom"} ) 
modify it to ->  Employee({"1","Employee with name: John"}, {"2","Employee with name: Tom"} )
public class Employee {
String id;
String info;
}   
public class People {
String id;
String name;
}    

@Mapping(source = "people.name", target = "displayName", qualifiedByName = "displayName")
List<Employee> toEmployee(List<People> people);

@Named("displayName")
default String getAudioId(String name){
return "Employee with name:" + name;
}

您需要将映射注释放置在Employee-to-People映射方法上,而不是列表映射方法上。

例如:

List<Employee> toEmployee(List<People> people);
@Mapping(source = "name", target = "info", qualifiedByName = "displayName")
Employee toEmployee(People people);

mapstruct将在toEmployee(List<People>)映射方法中使用toEmployee(People)映射方法。

相关内容

  • 没有找到相关文章

最新更新