The specific introduction and use of Mapstruct

I'm ABin-阿斌: write code for a lifetime, create a story for a lifetime, and build a youth at a glance. If you guys think my article is good, remember to click three times
insert image description here

1. Introduction to mapstruct

  • mapstruct is aEntity classThe mapping framework can safely assign the attributes of one entity class to another entity class through Java annotations. With mapstruct, you only need to define a mapper interface and declare the method that needs to be mapped. During the compilation process, mapstruct will automatically generate the implementation class of the interface to achieve the effect of mapping the source object to the target object.

2. Comparison between mapstruct and other mappings

  • There are roughly two types of entity class mapping frameworks: one is dynamic mapping through the java reflection mechanism at runtime; the other is dynamically generating getters/setters at compile time, and directly calling the compiled classes of the framework at runtime to implement entity mapping.
  • Since the mapstruct mapping is implemented during compilation,Therefore, compared with the runtime mapping framework, it has the following advantages:
  1. High security. Because the mapping from the source object to the target object is realized at the compile time, if the compiler can pass it, no error will be reported at the runtime.
  2. high speed. Fast speed means that the method of the implementation class is called directly during runtime, and reflection is not used for conversion during runtime.

3. Analysis of the underlying principle of mapstruct

  • mapstruct is implemented based on JSR 269, which is a specification introduced by JDK. With it, it is possible to process annotations at compile time, and read, modify and add content in the abstract syntax tree. JSR 269 uses Annotation Processor to process annotations during compilation. Annotation Processor is equivalent to a plug-in of the compiler, so it is also called plug-in annotation processing. To implement JSR 269, there are mainly the following steps:
  1. Inherit the AbstractProcessor class, rewrite the process method, and implement your own annotation processing logic in the process method.
  2. Create the javax.annotation.processing.Processor file in the META-INF/services directory to register your own implementation of the Annotation Processor

1. Java dynamic compilation

Java program compilation generally goes through the following process:

The process from Java source code to class file in the above figure is actually a relatively complicated process. The process can be described by the following figure:

The above process can be summarized as the following steps:

  1. Generate an abstract syntax tree. The Java compiler compiles the Java source code and generates an abstract syntax tree (Abstract Syntax Tree, AST).
  2. Call a program that implements the JSR 269 API. As long as the program implements the JSR 269 API, the implemented annotation processor will be invoked during compilation.
  3. Modify the abstract syntax tree. In the program that implements JSR 269 API, you can modify the abstract syntax tree and insert your own implementation logic.
  4. Generate bytecode. After the abstract syntax tree is modified, the Java compiler will generate a bytecode file corresponding to the modified abstract syntax tree.

4. Specific use

1. Dependency import

  • First, you need to import the dependency package,It mainly consists of two packages:
    • org.mapstruct:mapstruct:
      • Contains some necessary annotations, such as @Mapping . r If the JDK version we use is higher than 1.8, when we import dependencies in the pom, it is recommended to use the coordinates: org.mapstruct:mapstruct-jdk8 , which can help us take advantage of some new features of Java8 .
    • org.mapstruct:mapstruct-processor:
      • Annotation processor, which automatically generates mapper implementations based on annotations .
<properties>
        <!-- 版本依赖 -->
        <mapstruct.version>1.4.2.Final</mapstruct.version>
</properties>


<dependencies>
 <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>
        </dependency>
</dependencies>

2. Define an interface for use case mapping

  • Just create it under the entity class, which is easy to find
    insert image description here

scene one:

  • If the returned parameter attribute name is different from the mapping, then we have to map to the original
    insert image description here
    insert image description here

Scene two:

  • You can use expressions to get the information you want
  • For example: here we want to get a certain method of the tool class, then we can use this expression
    insert image description here

insert image description here

Scene three:

  • ignore: indicates that the current field of the mapping is ignored
    insert image description here

Reference article:

  • https://juejin.cn/post/6956190395319451679
  • https://www.cnblogs.com/mmzs/p/12735212.html

Guess you like

Origin blog.csdn.net/Mango_Bin/article/details/125168370