java MapStruct 字符串到列表映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37143179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MapStruct String to List mapping
提问by suresh
How would i map String to List and List to String?
我如何将字符串映射到列表和列表到字符串?
Consider we have following classess
考虑我们有以下课程
class People{
private String primaryEmailAddress;
private String secondaryEmailAddress;
private List<String> phones;
//getter and setters
}
class PeopleTO{
private List<String> emailAddress;
private String primaryPhone;
private String secondaryPhone;
//getter and setters
}
In Dozer and Orika, we can easily map with the following line of code
在 Dozer 和 Orika 中,我们可以使用以下代码行轻松映射
fields("primaryEmailAddress", "emailAddress[0]")
fields("secondaryEmailAddress", "emailAddress[1]")
fields("phones[0]", "primaryPhone")
fields("phones[1]", "secondaryPhone")
How i can do the same kind of mapping in MapStruct? Where would i find more examples on mapstruct?
我如何在 MapStruct 中进行相同类型的映射?我在哪里可以找到有关 mapstruct 的更多示例?
回答by lpacheco
The example below maps elements from the emailAddresslist in PeopleTOinto the primaryEmailAddressand secondaryEmailAddressproperties of People.
下面的示例将emailAddress列表中的元素映射PeopleTO到 的primaryEmailAddress和secondaryEmailAddress属性中People。
MapStruct can't directly map into collections, but it allows you to implement methods that run after a mapping to complete the process. I've used one such method for mapping the primaryPhoneand secondaryPhoneproperties of PeopleTOinto elements of the phoneslist in People.
MapStruct 不能直接映射到集合中,但它允许您实现在映射后运行的方法以完成该过程。我已经用于映射一种这样的方法primaryPhone和secondaryPhone性能PeopleTO入的元素phones列表People。
abstract class Mapper {
@Mappings({
@Mapping(target="primaryEmailAddress", expression="emailAddress != null && emailAdress.size() >= 1 ? emailAdresses.get(0) : null"),
@Mapping(target="secondaryEmailAddress", expression="emailAddress != null && emailAdress.size() >= 2 ? emailAdresses.get(1) : null"),
@Mapping(target="phones", ignore=true)
})
protected abstract People getPeople(PeopleTO to);
@AfterMapping
protected void setPhones(PeopleTO to, @MappingTarget People people) {
people.setPhones(new List<String>());
people.getPhones().add(to.primaryPhone);
people.getPhones().add(to.secondaryPhone);
}
}
回答by Vijay
I could see some examples here: https://github.com/mapstruct/mapstruct-examples
我可以在这里看到一些例子:https: //github.com/mapstruct/mapstruct-examples
Checkout this module for your specific requirement (Iterable to non-Iterable): https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-iterable-to-non-iterable
根据您的特定要求(可迭代到不可迭代)查看此模块:https: //github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-iterable-to-non-iterable
and another one here: http://blog.goyello.com/2015/09/08/dont-get-lost-take-the-map-dto-survival-code/
另一个在这里:http: //blog.goyello.com/2015/09/08/dont-get-lost-take-the-map-dto-survival-code/
Not sure if it is possible to map the non-iterable to Iterable.
不确定是否可以将不可迭代的映射到可迭代的。

