Mapstruct 简明教程

MapStruct - Environment Setup

MapStruct 是一个基于 Java 的库,因此第一个要求是在你的机器上安装 JDK。

MapStruct is a Java based library, so the very first requirement is to have JDK installed on your machine.

Step 1 - Setup Java Development Kit (JDK)

你可以从 Oracle 的 Java 站点下载 SDK 的最新版本 − Java SE Downloads. 你在下载文件中可以找到安装 JDK 的说明,按照给定的说明进行安装和配置。最后设置 PATH 和 JAVA_HOME 环境变量以引用包含 java 和 javac 的目录,通常分别为 java_install_dir/bin 和 java_install_dir。

You can download the latest version of SDK from Oracle’s Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.

如果你运行的是 Windows,并且已在 C:\jdk-9.0.1 中安装了 JDK,你必须在你的 C:\autoexec.bat 文件中输入以下行。

If you are running Windows and have installed the JDK in C:\jdk-9.0.1, you would have to put the following line in your C:\autoexec.bat file.

set PATH=C:\jdk-11.0.11\bin;%PATH%
set JAVA_HOME=C:\jdk-11.0.11

或者,在 Windows NT/2000/XP 中,你必须右键单击我的电脑,选择属性 → 高级 → 环境变量。然后,你将不得不更新 PATH 值并单击确定按钮。

Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button.

在 Unix(Solaris、Linux 等)上,如果 SDK 安装在 /usr/local/jdk-9.0.1 中并且你使用 C shell,你将必须输入以下内容到你的 .cshrc 文件中。

On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-9.0.1 and you use the C shell, you will have to put the following into your .cshrc file.

setenv PATH /usr/local/jdk-11.0.11/bin:$PATH
setenv JAVA_HOME /usr/local/jdk-11.0.11

或者,如果你使用诸如 Borland JBuilder、Eclipse、IntelliJ IDEA 或 Sun ONE Studio 这样的集成开发环境 (IDE),则必须编译并运行一个简单程序来确认 IDE 知道你在何处安装了 Java。否则,你必须按照 IDE 文档中给出的内容执行正确的设置。

Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE.

Step 2 - Install mapstruct library along with dependencies

MVNRepository 下载以下 jar,并在你的类路径中使用它们。

Download following jars from MVNRepository and use them in your classpath.

  1. mapstruct-1.5.0.Beta1.jar

  2. mapstruct-processor-1.5.0.Beta1.jar

确保正确设置此目录上的 CLASSPATH 变量,否则在运行应用程序时会遇到问题。

Make sure you set your CLASSPATH variable on this directory properly otherwise you will face a problem while running your application.

Step 3 - Setup Eclipse IDE

本教程中的所有示例都是使用 Eclipse IDE 编写的。因此,我们建议你应该在你机器上安装 Eclipse 的最新版本。

All the examples in this tutorial have been written using Eclipse IDE. So we would suggest you should have the latest version of Eclipse installed on your machine.

要安装 Eclipse IDE,请从 www.eclipse.org/downloads 下载最新的 Eclipse 二进制文件。下载安装文件后,将二进制分发包解压到方便的位置。例如,在 Windows 上的 C:\eclipse 中,或 Linux/Unix 上的 /usr/local/eclipse 中,最后相应地设置 PATH 变量。

To install Eclipse IDE, download the latest Eclipse binaries from www.eclipse.org/downloads. Once you download the installation, unpack the binary distribution into a convenient location. For example, in C:\eclipse on Windows, or /usr/local/eclipse on Linux/Unix and finally set PATH variable appropriately.

可以通过在 Windows 机器上执行以下命令启动 Eclipse,或者你只需双击 eclipse.exe

Eclipse can be started by executing the following commands on Windows machine, or you can simply double-click on eclipse.exe

%C:\eclipse\eclipse.exe

可以通过在 Unix(Solaris、Linux 等)机器上执行以下命令启动 Eclipse −

Eclipse can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine −

$/usr/local/eclipse/eclipse

成功启动后,如果一切正常,它应该显示以下结果 −

After a successful startup, if everything is fine then it should display the following result −

eclipsehomepage

Step 4 - Create a Maven Project

C:\MVN>mvn archetype:generate
-DgroupId = com.tutorialspoint.mapping
-DartifactId = mapping
-DarchetypeArtifactId = maven-archetype-quickstart
-DinteractiveMode = false

它将创建一个 maven 项目。现在,按如下方式更新 pom.xml 文件 −

It will create a maven project. Now update the pom.xml file as follows −

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint.mapping</groupId>
   <artifactId>mapping</artifactId>
   <packaging>jar</packaging>
   <version>1.0-SNAPSHOT</version>
   <name>mapping</name>
   <url>http://maven.apache.org</url>
   <dependencies>
      <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-engine</artifactId>
         <version>5.0.0</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.mapstruct</groupId>
         <artifactId>mapstruct</artifactId>
         <version>1.5.0.Beta1</version>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
               <source>11</source>
               <target>11</target>
               <annotationProcessorPaths>
                  <path>
                     <groupId>org.mapstruct</groupId>
                     <artifactId>mapstruct-processor</artifactId>
                     <version>1.5.0.Beta1</version>
                  </path>
               </annotationProcessorPaths>
            </configuration>
         </plugin>
      </plugins>
   </build>
</project>

运行以下命令以更新 maven 依赖关系并构建项目。

Run the following command to update maven dependencies and build project.

mvn package

一旦命令成功。将基于 maven 的项目作为 maven 项目导入到 Eclipse。其余的 Eclipse 将处理。

Once command is successful. Import the maven based project in Eclipse as a maven project. Rest Eclipse will handle.