Java Mapstruct:将多个源对象映射到子对象
给定一组四个对象,如:Java Mapstruct:将多个源对象映射到子对象,java,mapping,mapstruct,Java,Mapping,Mapstruct,给定一组四个对象,如: A{String one, B b} B{String two, String three} C{String one, String two} D{String three} 我希望生成如下映射: A cAndDToA(C c , D d); 我目前找不到一种方法来用来自C和D的数据填充a中的B对象 有人知道这个问题的解决方案吗,或者有更好的方法吗?您可以定义一种方法,从C和D填充B: B cAndDToB(C c, D d); 然后通过oncAndDToA
A{String one, B b}
B{String two, String three}
C{String one, String two}
D{String three}
我希望生成如下映射:
A cAndDToA(C c , D d);
我目前找不到一种方法来用来自C和D的数据填充a中的B对象
有人知道这个问题的解决方案吗,或者有更好的方法吗?您可以定义一种方法,从
C
和D
填充B
:
B cAndDToB(C c, D d);
然后通过oncAndDToA
手动调用:
@Mapper(decoratedWith=MyMapperDecorator.class)
public interface MyMapper {
A cAndDToA(C c, D d);
B cAndDToB(C c, D d);
}
public abstract class MyMapperDecorator implements MyMapper {
private final MyMapper delegate;
public MyMapperDecorator(MyMapper delegate) {
this.delegate = delegate;
}
@Override
public A cAndDToA(C c, D d) {
A a = delegate.cAndDToA( c, d );
a.setB( cAndDToB( c, d );
return a;
}
}
我们也将在目标方面为其提供支持。但是我们还没有做到:)工作很有魅力,谢谢你的建议。到目前为止,我对这个库印象深刻,并期待着实现这些嵌套映射。