我有两个映射器,A和B。A使用B,但源是相同的。我想从映射器A内部调用映射器B但我不知道怎么做。例子:
class A {
B fieldB;
}
@Mapper(componentModel = "spring")
public interface BMapper {
//mapping rules here
B mapToDomain(CommonEntity entity);
}
@Mapper(componentModel = "spring", uses = BMapper.class)
public interface AMapper {
//mapping rules here
//HERE I WANT TO INVOKE MAPPER B and for B to map to specific A.B target
A mapToDomain(CommonEntity entity);
}
我试着表达,但它不起作用。
Mapper#uses
中的映射器只有在被MapStruct显式使用时才会被注入到实现中,否则它们将被忽略。
你能做的就是告诉MapStruct你想把源参数映射到fieldB
。
@Mapper(componentModel = "spring", uses = BMapper.class)
public interface AMapper {
@Mapping(target = "fieldB", source = "entity"
A mapToDomain(CommonEntity entity);
}