Mapstruct to update values without overwriting

南楼画角 提交于 2021-02-10 05:11:42

问题


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

For example:

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;
 ...
}

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.

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


回答1:


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.



来源:https://stackoverflow.com/questions/52755301/mapstruct-to-update-values-without-overwriting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!