如何使用 MapStruct 应用两个参数映射对象的函数?

How to apply function of two arguments mapping objects using MapStruct?

有两个来源 classes A 和 B

class A {
    public Double x;
    public Double y;
}

class B {
    public Double x;
    public Double y;
}

和另一个目标 class C

class C {
    public Double x;
    public Double y;
}

很清楚如何将A映射到C或B映射到C。

是否可以将一些功能映射到目标对象,例如源对象的添加或 pow,以便生成的代码看起来像这样

C.x = A.x + B.x
C.y = A.y + B.y

C.x = Math.pow(A.x, B.x)
C.y = Math.pow(A.y, B.y)

这可以通过使用表达式来完成。

@Mapper
public interface MyMapper {

    @Mapping(target = "x", expression = "java(a.x + b.x)")
    @Mapping(target = "y", expression = "java(a.y + b.y)")
    C map(A a, B b);
}

@Mapper
public interface MyMapper {

    @Mapping(target = "x", expression = "java(Math.pow(a.x, b.x))")
    @Mapping(target = "y", expression = "java(Math.pow(a.y, b.y))")
    C map(A a, B b);
}

可以在参考文档中找到有关表达式的更多信息here