如何使用MapStruct 1.2有条件地映射属性? [英] How can I map properties conditionally with MapStruct 1.2?

查看:86
本文介绍了如何使用MapStruct 1.2有条件地映射属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MapStruct 1.2是否可以将具有特定值的源属性映射到目标中的特定不同值?

Is it possible with MapStruct 1.2 to map a source property with a specific value to a specific different value in the target?

我考虑这样的事情:

public abstract class JiraKpmMapper {
    @Mappings({
    @Mapping(source = "mySource.propA", target = "myTarget.propX")
    })
    @ValueMappings({
        @ValueMapping(source = "ABC", target = "XYZ"),
        @ValueMapping(source = "123", target = "789")
    })
    public abstract MyTarget source2Target(final MySource mySource);

}

因此,当MapStruct在映射期间看到mySource.propA具有值"ABC"时,myTarget.propX需要设置为值"XYZ",依此类推.

So that when MapStruct sees during the mapping that when mySource.propA has the value "ABC" myTarget.propX needs to be set to value "XYZ" and so forth.

更准确地说,我什至希望更详细地阐述一些内容:目标应该是一个类的避风港,必须将所得的目标值分为三个属性.例如,如果mySource.propA的值为"ABC",则目标myTarget的值应为"V01.123.456.AB".该值又应分为preValue,middleValue和endValue:

To be more precisely, I even want something more elaborated: The target should be a class haven three properties in which the resulting target value must be split into. For instance, if mySource.propA has the value "ABC" the target myTarget should get a value like "V01.123.456.AB". This value in turn shall be split up in a preValue, a middleValue and an endValue:

preValue ="V01"

preValue = "V01"

middleValue ="123.456"

middleValue = "123.456"

endValue ="AB"

endValue = "AB"

因此,没有包含完整结果字符串的属性.

whereby there's no property holding the complete result string.

这就是为什么我已经编写了一个自定义映射器,并告诉MyMapper通过以下方式使用它的原因

That's why I already wrote a custom mapper and I tell the MyMapper to use it via

@Mapper(componentModel = "spring", uses = MyCustomMapper.class)

到目前为止,该方法仍然有效,但是当souzrce随附"ABC"时,我无法告诉MyCustomMapper将"V01.123.456.AB"放入目标.

This works so far but I can't achieve it to tell the MyCustomMapper to put "V01.123.456.AB" into the target when the souzrce comes with "ABC".

推荐答案

使用MapStruct不能真正做到这一点. @ValueMapping 批注用于映射 Enum .

You can't really do that with MapStruct. The @ValueMapping annotation is for mapping of Enum(s).

要实现所需的功能,您需要在 @BeforeMapping @AfterMapping 中进行操作.

In order to achieve what you are looking for you would need to do that in @BeforeMapping or @AfterMapping.

例如,您可以执行以下操作:

For example you can do something like:

@Mapper
public interface JiraKpmMapper {

    @BeforeMapping
    default void beforeMapping(@MappingTarget MyTarget target, MySource source) {
        if (source.getPropY().equals("ABC") {
            target.setPropX("V01.123.456.AB");
        }
    }

    @Mapping(target = "propX", ignore = true) // This is now mapped in beforeMapping
    MyTarget source2Target(final MySource mySource);
}

您的自定义映射器应该具有一个 @AfterMapping .将 propX 转换为类的位置.您甚至可以将其作为我编写的 @BeforeMapping 的一部分并直接创建您的类(或调用将从 String 转换为类的方法)

Your custom mapper then should have an @AfterMapping. Where you would convert the propX into your class. You can even do this as part of the @BeforeMapping I wrote and directly create your class (or invoke the method that does the conversion from String into a class)

这篇关于如何使用MapStruct 1.2有条件地映射属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆