这真的取决于您使用的是哪个版本的 MapStruct。如果您使用的是 1.2.0.Beta 或更高版本,您可以在 EntityMapper 接口上定义嵌套属性:
@Mapper
public interface EntityMapper {
@Mapping(target = "anotherEntity", source = "anotherEntityDTO")
@Mapping(target = "anotherEntity.propE", source = "anotherEntityDTO.propD")
Entity map(EntityDDTO dto);
}
另一个选项(如果您使用低于 1.2.0.Beta 的版本,则必须这样做)是在您的 EntityMapper 中添加一个新方法,例如:
@Mapper
public interface EntityMapper {
@Mapping(target = "anotherEntity", source = "anotherEntityDTO")
Entity map(EntityDDTO dto);
@Mapping(target = "propE", source = "propD")
AnotherEntity map(AnotherEntityDTO);
}
或者您可以为AnotherEntity 定义一个新的映射器AnotherEntityMapper 并使用@Mapper(uses = {AnotherEntityMapper.class}):
@Mapper
public interface AnotherEntityMapper {
@Mapping(target = "propE", source = "propD")
AnotherEntity map(AnotherEntityDTO);
}
@Mapper(uses = {AnotherEntityMapper.class}
public interface EntityMapper {
@Mapping(target = "anotherEntity", source = "anotherEntityDTO")
Entity map(EntityDDTO dto);
}
这真的取决于您的用例。如果你需要在其他地方做AnotherEntity和AnotherEntityDTO之间的映射,我建议你使用一个新的接口,这样你就可以在需要的地方重复使用它