Mapstruct 在不覆盖的情况下更新值 [英] Mapstruct to update values without overwriting

查看:169
本文介绍了Mapstruct 在不覆盖的情况下更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法指示 MapStruct 不覆盖目标中的值?

Is there a way to instruct MapStruct to not overwrite values in the target?

例如:

public interface IMyMapper {
    IMyMapper INSTANCE = Mappers.getMapper(IMyMapper.class);
    @Mappings({
            @Mapping(target = "foo", source = "source.FOO"),
            @Mapping(target = "bar", source = "source.BAR2"),
    })
    void updateTargetEntity(@MappingTarget MyTarget target , MySource source);
}

class MyTarget {
 String a;
 String b;
 ...
}

class MySource {
 String a;
 String b;
 ...
}

例如 target 将有 a = "asdf",而 source 将有 a = "zzz",我不希望 zzz 覆盖 asdf.

Where for instance target will have a = "asdf", and source will have a = "zzz", I don't want zzz to overwrite asdf.

注意:我不想总是忽略值,我只是不想在它们存在时覆盖它们.

NOTE: I don't want to always ignore values, I just don't want to overwrite them if they exist.

我尝试在这里查看几种映射策略:http://mapstruct.org/documentation/stable/reference/html/ 但它们似乎都不适合.

I tried to look at the several mapping strategies here: http://mapstruct.org/documentation/stable/reference/html/ but none of them seems to fit.

推荐答案

我们目前正在讨论这个问题.有一个 PR 待定.留意那个.对于 BeanMappings,所有当前策略都适用于 BeanMapping 级别,而不是属性级别.因此,NullValueMappingStrategy 会告诉您当输入 bean(或 Map,或 MapMapping、IterableMapping 中的 List)为空时会发生什么.没有(直到不久)您正在寻找的东西.

We are currently discussing this. There's a PR pending. Keep an eye out for that one. For BeanMappings, all the current strategies apply on the BeanMapping level, not on the property level. So, the NullValueMappingStrategy tells you what happens when an input bean (or Map, or List in case of MapMapping, IterableMapping) is null. There was (up till soon) no such thing what you are looking for.

这篇关于Mapstruct 在不覆盖的情况下更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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