仅在表达式子句中使用另一个 MapStruct 映射器
Use another MapStruct mapper only inside an expression clause
我有一个映射器,对于目标 class 的特定属性,需要从源对象内的对象列表中选择一个,并使用不同的映射器映射它 class .
简化了很多,Game
class 包含一个 Transaction
对象列表,我的 GameMapper
class 看起来像这样:
@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
}
问题是,EventMapper
生成为 GameMapperImpl
内的 private final EventMapper eventMapper;
属性,但 TransactionMapper
不是,因此构建失败,因为 MapStruct 找不到 transactionMapper
.
我最好的猜测是,这是由于 GameMapper
中没有其他方法明确使用 TransactionMapper
,因此 Mapstruct 决定不需要它并且不会在实现中注入它。
所以...有什么方法可以强制 MapStruct 在 uses
子句中包含映射器,即使它们看起来没有被使用,或者有任何其他方法可以解决这个问题?
我设法找到了一个 hacky 解决方案。通过向 GameMapper
添加一个 List<TransactionDto> dummy(List<Transaction> transaction);
方法,我欺骗了 MapStruct 认为我真的在使用 TransactionMapper
来做某事并且它已经成功地将它添加到 GameMapperImpl
.
不过,我仍然对非 hacky 解决方案持开放态度:)
My best guess is that this is due to no other methods in GameMapper
using TransactionMapper
explicitly, so Mapstruct decides that it's not needed and doesn't inject it in the implementation.
没错。如果 MapStruct 未使用它们,MapStruct 将不会从 Mapper#uses
注入映射器。
你可以做的是使用摘要 class。
例如
@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
}
我有一个映射器,对于目标 class 的特定属性,需要从源对象内的对象列表中选择一个,并使用不同的映射器映射它 class .
简化了很多,Game
class 包含一个 Transaction
对象列表,我的 GameMapper
class 看起来像这样:
@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
}
问题是,EventMapper
生成为 GameMapperImpl
内的 private final EventMapper eventMapper;
属性,但 TransactionMapper
不是,因此构建失败,因为 MapStruct 找不到 transactionMapper
.
我最好的猜测是,这是由于 GameMapper
中没有其他方法明确使用 TransactionMapper
,因此 Mapstruct 决定不需要它并且不会在实现中注入它。
所以...有什么方法可以强制 MapStruct 在 uses
子句中包含映射器,即使它们看起来没有被使用,或者有任何其他方法可以解决这个问题?
我设法找到了一个 hacky 解决方案。通过向 GameMapper
添加一个 List<TransactionDto> dummy(List<Transaction> transaction);
方法,我欺骗了 MapStruct 认为我真的在使用 TransactionMapper
来做某事并且它已经成功地将它添加到 GameMapperImpl
.
不过,我仍然对非 hacky 解决方案持开放态度:)
My best guess is that this is due to no other methods in
GameMapper
usingTransactionMapper
explicitly, so Mapstruct decides that it's not needed and doesn't inject it in the implementation.
没错。如果 MapStruct 未使用它们,MapStruct 将不会从 Mapper#uses
注入映射器。
你可以做的是使用摘要 class。
例如
@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
}