A library that helps convert Entity (or Domain) to DTO. Famous Mapping libraries include BeanUtils from Structs1.x and ModelMapper / Dozer.
The feature of MapStruct is that the code is automatically generated using the annotation processor like Lombok. The feature is that it is as fast as mapping using getters / setters on its own.
The performance is introduced in the following article. Performance of Java Mapping Frameworks
It's subjective, but I find the code intuitive and easy to understand, not only in terms of performance, but also when compared to other mapping tools.
It's such a nice mapping tool, but I've been stuck with it for a while & I couldn't find a similar article, so I'm writing this article.
According to the Lombok Changelog, 1.8.2 seems to fix a bug in Integration with MapStruct.
・ Pleiades Eclipse 2018-09 (* Lombok 1.8.2) ・ AdoptOpenJDK 11 ・ Lombok 1.8.2 -MapStruct 1.2.0.Final and 1.3.0.Beta1
In conclusion, it works with the sample code in mapstruct-lombok and It didn't work in the MapStruct Readme (I was addicted to it).
Describe the pom as follows, ** Select a project in eclipse and run ⇒ maven install or maven test ** will generate a coat The test code provided in mapstruct-lombok succeeds.
Unfortunately, eclipse's automatic builds and clean builds don't generate code: sob: IDE Support is available and is designed to run "annotation processors" automatically. ** This is because if you set the essential "annotation processors" to Maven, it will not work: innocent: **
pom.xml
<properties>
<org.mapstruct.version>1.3.0.Beta1</org.mapstruct.version> ※ 1.2.0.Confirmed that Final also works
<org.projectlombok.version>1.18.2</org.projectlombok.version>
</properties>
<!--abridgement-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>* It will be different from the Readme
<version>${org.mapstruct.version}</version>
<scope>provided</scope>* It is described to be executed at compile time.
</dependency>
<!--abridgement-->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>* Annotation described in the point Readme-processor not listed
<version>3.6.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.6</source> <!-- or higher, depending on your project -->
<target>1.6</target> <!-- or higher, depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>* ← At least, it doesn't seem to work on Java 11.
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
In the official documentation, the using_mapstruct_on_java_9 section states that "java.annotations.common" should be enabled. The java.annotations.common was Java 9 and was renamed to java.xml.ws .annotation, ** Wow, in Java 11, java.xml.ws.annotation has been removed: innocent:: innocent:: innocent: **
For now, if you write the OK pattern pom.xml and run the maven build, the code will be generated We have confirmed that it works as expected, but please understand that there may be risks.
This article has described how to use MapStruct with lombok and Java 11. For the actual usage of MapStruct, please refer to other Qiita articles and the sample code of the head family.
There are some points that fit, but It's hundreds of times more fun to write code than writing getters / setters on your own to reduce the chance of bugs.
I hope you can contribute a little to your Java life.
Recommended Posts