2016-02-29 300 views
4

给予相同的一组四个对象: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); 

我目前还不能找到一种方法来填充B中的对象与来自C和D的数据。

有没有人知道这个问题的解决方案,或有更好的方法?

回答

3

你可以定义一个方法用于填充从CBD

B cAndDToB(C c, D d); 

,然后通过手动调用这个decoratorcAndDToA

@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(PersonMapper 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; 
    } 
} 

我们将提供对nested mappings支持目标方也是如此。但我们还没有:)

+0

工作就像一个魅力,感谢您的建议。到目前为止,我对这个库印象非常深刻,并期待着这些嵌套映射的实现。 – Ghen