MapStruct 官方文档 中给出的maven-compiler-plugin
插件的版本是3.5.1
。
在 MapStruct 结合 lombok 使用时仍然会报错,不会编译出 Mapper 的实现类。
调整插件版本至3.6.1
或以上即可解决这个问题。
<properties>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
两种常用的 Mapper 获取方式:
通过 mappers factory
import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @Mapper public interface YourMapper { YourMapper INSTANCE = Mappers.getMapper(YourMapper.class); }
componentModel
指定为spring
,使用依赖注入(@Autowired
)的方式获取import org.mapstruct.Mapper; @Mapper(componentModel = "spring") public interface YourMapper { }