Mapstruct 未在生成的源文件中更新其 getter 和 setter

Mapstruct is not updating its getters and setters in the generated source files

我有一个实体 属性 我以前是这样写的 私人长加护病房;

我正在使用 mapstruct:

这是我对所述实体的映射器:

@Mapper(componentModel = "spring")
public interface ProtectionQueryMapper extends EntityMapper<ProtectionQueryDto, Protection> {

    ProtectionQueryDto toDto(Protection protection);

    Protection toEntity(ProtectionQueryDto protectionQueryDto);

    List<Protection> toEntity(List<ProtectionQueryDto> protectionQueryDtos);

    List<ProtectionQueryDto> toDto(List<Protection> protections);

}
public interface EntityMapper<D, E> {

    E toEntity(D dto);

    D toDto(E entity);

    List<E> toEntity(List<D> dtoList);

    List<D> toDto(List<E> entityList);
}

我遇到的问题是我想将 属性 从 ICU 更改为 icu,我这样做了,但导致了这个错误:

nested exception is java.lang.NoSuchMethodError:

Protection.getICU()Ljava/lang/Long;

似乎 mapstruct 生成它的 getter 和 setter 是基于: private Long ICU; 生成方法,如 setICU 和 getICU。 但是现在我已经将 属性 从 ICU 更改为 icu mapstruct 没有将其方法更新为 setIcugetIcu.

我无法手动更改 mapstruct 生成的文件。

这也是我的 pom.xml(至少是关于 mapstruct 的部分)

<dependency>
  <groupId>org.mapstruct</groupId>
  <artifactId>mapstruct</artifactId>  
  <version>1.3.0.Final</version>
</dependency>

              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.3.0.Final</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <compilerArg>
                            <arg>-Amapstruct.defaultComponentModel=spring</arg>
                        </compilerArg>
                    </compilerArgs>
                </configuration>
            </plugin>

知道如何让 mapstruct 更新其生成的源文件吗?

出于某种原因,项目的重新编译没有 运行 注释处理器。 MapStruct 由 Java 编译器调用,maven-compiler-plugin 负责使用生成的 类.

清理文件夹

mvn clean compile总是有效的。但是,如果进行更改然后执行 mvn compile 没有,我会尝试使用最新版本的 maven-compiler-plugin,如果仍然不起作用,请为插件创建错误报告。

如果您将 Lombok 用于您的实体和 DTO,则必须像这样更新您的 pom.xml:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <arg>-Amapstruct.defaultComponentModel=spring</arg>
                    </compilerArgs>
                </configuration>
            </plugin>

然后 Mupstruct 将能够看到它们的 getter 和 setter。

(您可以查看我的 project and its demo 以了解实际效果。)

需要按顺序先提供lombok插件,再提供mapstruct-processor插件就这样

<configuration>
    <source>1.8</source>
    <target>1.8</target>
    <annotationProcessorPaths>
        <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </path>
        <path>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.4.2.Final</version>
        </path>
    </annotationProcessorPaths>
</configuration>