问题描述:

MapperStruct生成的实现了为空

解决方案

按照在MapperStruct官网Installation – MapStruct中的方法配置后,生成的实现了是空的,如下:

 @Override
   public DeployHistory toEntity(DeployHistoryDto arg0) {
       if ( arg0 == null ) {
           return null;
       }
       DeployHistory deployHistory = new DeployHistory();

       return deployHistory;
   }

   @Override
   public DeployHistoryDto toDto(DeployHistory arg0) {
       if ( arg0 == null ) {
           return null;
       }
       DeployHistoryDto deployHistoryDto = new DeployHistoryDto();

       return deployHistoryDto;
   }

原因分析:

使用的lombok版本过高

解决方案:

我在查阅了很多资料后终于解决问题了。需要在pom.xml中添加插件lombok-mapstruct-binding,完整的配置如下:

<properties>
  <lombok.version>1.18.24</lombok.version>
  <mapstruct.version>1.5.5.Final</mapstruct.version>
  <lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
</properties>
<dependencies>
   <!--mapStruct依赖-->
   <dependency>
       <groupId>org.mapstruct</groupId>
       <artifactId>mapstruct</artifactId>
       <version>${mapstruct.version}</version>
   </dependency>
   <dependency>
       <groupId>org.mapstruct</groupId>
       <artifactId>mapstruct-processor</artifactId>
       <version>${mapstruct.version}</version>
       <optional>true</optional>
   </dependency>
</dependencies>
<build>
   <plugins>
      <plugin>
       <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
        <source>17</source><!-- depending on your project -->
        <target>17</target><!-- depending on your project -->
            <annotationProcessorPaths>
                <path>
                    <groupId>org.mapstruct</groupId>
                    <artifactId>mapstruct-processor</artifactId>
                    <version>${mapstruct.version}</version>
                </path>
                <!-- other annotation processors -->
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>${lombok.version}</version>
                </path>
                <path>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok-mapstruct-binding</artifactId>
                    <version>${lombok-mapstruct-binding.version}</version>
                </path>
            </annotationProcessorPaths>
        </configuration>
      </plugin>
   </plugins>
 </build>

欢迎大家关注我的号和博客