【发布时间】:2017-11-09 22:25:33
【问题描述】:
我了解Mapstruct 允许我定义自己的映射器逻辑,我这样做是这样的:
@Mapper(componentModel = "spring")
public abstract class ProjectMapper {
public ProjectInfo map(ProjectEntity projectEntity) {
ProjectInfo projectInfo = new ProjectInfo();
projectInfo.setName(projectEntity.getName());
projectInfo.setDescription(projectEntity.getDescription());
// Specific logic that forces me to define it myself
if (projectEntity.getId() != null) {
projectInfo.setId(projectEntity.getId());
}
if (projectEntity.getOrganisation() != null) {
projectInfo.setOrganisation(projectEntity.getOrganisation().getName());
}
return projectInfo;
}
}
它工作得很好,但我也想要Mapstruct 生成的映射器,但它们必须在接口中定义,有没有办法将这两种映射器类型分组?
【问题讨论】:
-
MapStructs 映射器必须在接口中定义是什么意思?您也可以在abstract类中定义主题,MapStruct将实现所有抽象方法。我不确定你的具体逻辑是否真的像它看起来的那样,但MapStruct可以生成完全相同的方法。