问题
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