它确实取决于您使用的是哪个版本的MapStruct。 (如果您使用的版本低于1.2.0.Beta和必须的)
@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
接口的嵌套的属性是在增加新的方法你EntityMapper
这样的:
@Mapper
public interface EntityMapper {
@Mapping(target = "anotherEntity", source = "anotherEntityDTO")
Entity map(EntityDDTO dto);
@Mapping(target = "propE", source = "propD")
AnotherEntity map(AnotherEntityDTO);
}
,或者你可以定义一个新的映射AnotherEntityMapper
为AnotherEntity
和使用@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
之间的映射,我会建议使用新界面,以便您可以在需要的地方重复使用它