本文介绍了ModelMapper跳过字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在UserDTO
和User
之间映射,但要排除一个字段,例如city
.我该怎么做,因为虽然这种方法行得通,但它却没有:
I would like to map between UserDTO
and User
, but excluding one field, say city
. How can I do that, cause I though that this approach would work, but it doesn't:
ModelMapper modelMapper = new ModelMapper();
modelMapper.typeMap(UserDTO.class,User.class).addMappings(mp -> {
mp.skip(User::setCity);
});
推荐答案
由于通用参数,我们无法使用lambda表达式.
Because of the generic parameters, we couldn't use the lambda expression.
ModelMapper modelMapper = new ModelMapper();
modelMapper.addMappings(new PropertyMap<Dto, Source>() {
@Override
protected void configure() {
skip(destination.getBlessedField());
}
});
这篇关于ModelMapper跳过字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!