LuT*_*ieR 6 java spring mapstruct
我有一个映射器,对于目标类的特定属性,需要从源对象内的对象列表中选择一个,并使用不同的映射器类来映射它。
简化很多,该类Game包含一个对象列表Transaction,我的GameMapper类如下所示:
@Component
@Mapper(injectionStrategy = InjectionStrategy.CONSTRUCTOR, uses = {TransactionMapper.class, EventMapper.class})
public interface GameMapper {
@Mapping(target = "transaction",
expression = "java(transactionMapper.transactionToDto(findTransactionForPlayer(game, idPlayer)))")
GameResultDto gameToGameResultDto(Game game, Long idPlayer);
// Some more methods
}
Run Code Online (Sandbox Code Playgroud)
问题是,EventMapper被生成为private final EventMapper eventMapper;内部的属性GameMapperImpl,但TransactionMapper不是,因此构建失败,因为 MapStruct 找不到transactionMapper。
我最好的猜测是,这是由于没有显式GameMapper使用其他方法TransactionMapper,因此 Mapstruct 决定不需要它并且不会将其注入到实现中。
那么...有没有办法强制 MapStruct 在子句中包含映射器uses,即使看起来它们没有被使用,或者有任何其他方法来解决这个问题?
Fil*_*lip 10
我最好的猜测是,这是由于没有显式
GameMapper使用其他方法TransactionMapper,因此 Mapstruct 决定不需要它并且不会将其注入到实现中。
那是对的。Mapper#uses如果 MapStruct 不使用映射器,则 MapStruct不会注入映射器。
您可以做的是使用抽象类。
例如
@Mapper(injectionStrategy = InjectionStrategy.CONSTRUCTOR, componentModel = "spring", uses = {TransactionMapper.class, EventMapper.class})
public abstract class GameMapper {
@Autowired
protected TransactionMapper transactionMapper;
@Mapping(target = "transaction",
expression = "java(transactionMapper.transactionToDto(findTransactionForPlayer(game, idPlayer)))")
public abstract GameResultDto gameToGameResultDto(Game game, Long idPlayer);
// Some more methods
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8650 次 |
| 最近记录: |