我按照评论中Gunnar的建议解决了我的问题。
我移至MapStruct
1.2.0.Beta1并创建了一个UserMapperResolver,如下所示
@Componentpublic class UserMapperResolver { @Autowired private UserRepository userRepository; @ObjectFactory public User resolve(baseUserDto dto, @TargetType Class<User> type) { return dto != null && dto.getId() != null ? userRepository.findOne(dto.getId()) : new User(); }}
然后在UserMapper中使用它:
@Mapper(uses = { UserMapperResolver.class })public interface baseUserMapper { baseUserDto map(User user); User map(baseUserDto baseUser);}
现在生成的代码是:
@Override public User map(baseUserDto baseUser) { if ( baseUser == null ) { return null; } User user = userMapperResolver.resolve( baseUser, User.class ); user.setId( baseUser.getId() ); user.setSocialMediaProvider( baseUser.getSocialMediaProvider() );...}
效果很好 !
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)