我有一个我用过这样写的属性的实体私人长ICU;
我正在使用映射:
这是我上述实体的映射者:
@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 GO ICU更改属性,这导致了此错误:
嵌套异常是java.lang.nosuchmethoderror:
protection.geticu((ljava/lang/long;
似乎MAPSTRUCT基于: private Long ICU;
生成诸如seticu和geticu之类的方法。但是,现在我已将属性从ICU更改为ICU Mapstruct并未将其方法更新为setIcu
和getIcu
。
我无法手动更改mapstruct
生成的文件。
也是我的pom.xml(至少有关映射的零件(
<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>
任何想法如何使映射更新其生成的源文件?
您需要先按顺序提供 LOMBOK 插件,然后 mapstruct-Prococessor 插件,就像这样。
<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>
由于某种原因,项目的重新编译没有运行注释处理器。MAPSTRUCT由Java编译器调用,Maven-Compiler-Plugin负责使用生成的类清洁文件夹。
执行mvn clean compile
始终可以工作。但是,如果进行更改,然后进行mvn compile
不进行,我会尝试使用Maven-Compiler-Plugin的最新版本,如果仍然不起作用,请为插件创建错误报告。
感谢https://stackoverflow.com/users/10467757/adil-aslam-sachwani,他的回答是在Lombok之后设置Mapsstruct处理器。它对我有用,但我和gradle一起使用,所以在这种情况下,应该这样:
implementation 'org.mapstruct:mapstruct:1.5.2.Final'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.2.Final'
如果您使用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将能够看到他们的Getters和Setter。
(您可以检查我的项目和它的演示以查看此操作。(
我知道这是一个古老的问题,但是在编辑pom.xml之后"运行"按钮,这将导致项目重建,然后您生成的来源将再次是最新的