一样平常情况下只需要按照官网的导入即可,但假如同时使用了lombok,则需要小心。具体信息可以检察官网文档:MapStruct 1.5.5.Final 集成lombok
注意:在编译测试的时候,一定先清算再编译。这样可以解决80%的报错问题假如配置不正确,则可能在编译的时候出现如下问题:
出现如下问题请清算后再编译复制代码
- [WARNING] 批注处理不适用于隐式编译的文件。
- 使用 -implicit 指定用于隐式编译的策略。
14.2. Lombok
MapStruct works together with Project Lombok as of MapStruct 1.2.0.Beta1 and Lombok 1.16.14.
MapStruct 与 MapStruct 1.2.0.Beta1 和 Lombok 1.16.14 的 Lombok 项目一起工作。
MapStruct takes advantage of generated getters, setters, and constructors and uses them to generate the mapper implementations.
MapStruct 利用生成的 getter、setter 和构造函数,并使用它们来生成 mapper 实现。
Lombok 1.18.16 introduces a breaking change (changelog). The additional annotation processor (Maven) must be added otherwise MapStruct stops working with Lombok. This resolves the compilation issues of Lombok and MapStruct modules.lombok-mapstruct-binding14.2.1. Set up 设置
Lombok 1.18.16 引入了一个重大更改(更新日志)。必须添加额外的解释处理器 (Maven),否则 MapStruct 将克制与 Lombok 一起工作。这将解决 Lombok 和 MapStruct 模块的编译问题。lombok-mapstruct-binding复制代码
- <path>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok-mapstruct-binding</artifactId>
- <version>0.2.0</version>
- </path>
The set up using Maven or Gradle does not differ from what is described in Set up. Additionally, you need to provide Lombok dependencies.
使用 Maven 或 Gradle 的设置与 设置中形貌的设置没有区别。别的,您需要提供 Lombok 依赖项。
Example 116. Maven configuration
例 116.Maven 配置Example 117. Gradle configuration (3.4 and later)复制代码
- <properties>
- <org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
- <org.projectlombok.version>1.18.16</org.projectlombok.version>
- <maven.compiler.source>1.8</maven.compiler.source>
- <maven.compiler.target>1.8</maven.compiler.target>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.mapstruct</groupId>
- <artifactId>mapstruct</artifactId>
- <version>${org.mapstruct.version}</version>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>${org.projectlombok.version}</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.1</version>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- <annotationProcessorPaths>
- <path>
- <groupId>org.mapstruct</groupId>
- <artifactId>mapstruct-processor</artifactId>
- <version>${org.mapstruct.version}</version>
- </path>
- <path>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>${org.projectlombok.version}</version>
- </path>
- <path>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok-mapstruct-binding</artifactId>
- <version>0.2.0</version>
- </path>
- </annotationProcessorPaths>
- </configuration>
- </plugin>
- </plugins>
- </build>
例 117.Gradle 配置(3.4 及更高版本)The usage combines what you already know from Defining a mapper and Lombok.复制代码
- dependencies {
- implementation "org.mapstruct:mapstruct:${mapstructVersion}"
- compileOnly "org.projectlombok:lombok:1.18.16"
- annotationProcessor "org.projectlombok:lombok-mapstruct-binding:0.2.0"
- annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
- annotationProcessor "org.projectlombok:lombok:1.18.16"
- }
该用法结合了您在定义映射器和 Lombok 中已经了解的内容。
Example 118. Usage of MapStruct with Lombok
例 118.MapStruct 在 Lombok 中的使用A working example can be found on the GitHub project mapstruct-lombok.复制代码
- @Data
- public class Source {
- private String test;
- }
- public class Target {
- private Long testing;
- public Long getTesting() {
- return testing;
- }
- public void setTesting( Long testing ) {
- this.testing = testing;
- }
- }
- @Mapper
- public interface SourceTargetMapper {
- SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );
- @Mapping( source = "test", target = "testing" )
- Target toTarget( Source s );
- }
可以在 GitHub 项目 mapstruct-lombok 上找到一个工作示例。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |