指定第三级映射器的多个源字段
示例
public class Source {
String fromPersonFirstName;
String fromPersonLastName;
String toPersonFirstName;
String toPersonLastName;
String message;
}
public class Target {
Person from;
Person to;
String message;
}
public class Person {
String firstName;
String lastName;
}
@Mapper(componentModel = "spring")
public interface PersonMapper {
Person toPerson(String firstName, String lastName);
}
现在对问题
进行了对象,采用尽可能多的映射方式是什么干净的方式?不使用表情?
理想情况下,我会告诉MapStruct进行以下操作
@Mapper(componentModel = "spring", uses = PersonMapper.class)
public interface ExampleMapper {
@Mapping(target = "from", source = "fromPersonFirstName, fromPersonLastName")
@Mapping(target = "to", source = "toPersonFirstName, toPersonLastName")
Target toTarget(Source s);
}
,我可以注释shorsMapper
和toperson
,然后对于totarget
method code> filect> filect> filesifiedbyname ='' theclassNameValue,themEthodNameValue“ 。但是我仍然不知道如何告诉使用哪种源字段使用。
还有什么选择?我可以使用本地方法toperson
,filesifiedbyname
。但是,我必须以某种方式导入其他映射器。也许更改exipplemapper
以抽象并自动自动映射。但是我仍然无法指定多个字段。
我知道表达式
可能是为了填补此空白。但是...如果可能的话,我宁愿永远不会使用它。除非替代方案要复杂得多。
Sample Objects
public class Source {
String fromPersonFirstName;
String fromPersonLastName;
String toPersonFirstName;
String toPersonLastName;
String message;
}
public class Target {
Person from;
Person to;
String message;
}
public class Person {
String firstName;
String lastName;
}
@Mapper(componentModel = "spring")
public interface PersonMapper {
Person toPerson(String firstName, String lastName);
}
The Question
Now, what would be a clean way of employing as much as mapstruct as possible? Without using expressions?
Ideally I'd tell mapstruct to do the following
@Mapper(componentModel = "spring", uses = PersonMapper.class)
public interface ExampleMapper {
@Mapping(target = "from", source = "fromPersonFirstName, fromPersonLastName")
@Mapping(target = "to", source = "toPersonFirstName, toPersonLastName")
Target toTarget(Source s);
}
Alternatively I could annotate PersonMapper
and toPerson
, then for the toTarget
method qualifiedByName = "TheClassNameValue, TheMethodNameValue"
. But I still don't know how to tell the method which source fields to use.
What would be another option? I could use a local method toPerson
, qualifiedByName
. But then I'd have to import the other mapper in some way. Maybe change the ExampleMapper
to abstract and autowire the other mapper. But I still can't specify multiple fields.
I know that expression
is probably meant to fill in this gap. But ... I would rather never use it if possible. Unless the alternative is considerably more complicated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案1,基于限定器的映射方法选择/a>
生成的代码:
与自定义方法的替代方案:
生成的代码:
单独的映射器:
解决方案2,表达式
生成的代码:
解决方案3,常规映射
生成的代码:
Solution 1, Mapping method selection based on qualifiers
Generated code:
Alternative with custom methods:
Generated code:
With separate mapper:
Solution 2, Expression
Generated code:
Solution 3, regular mapping
Generated code: