summary:In actual projects, we often need to convert between entities such as PO to DTO, DTO to PO, etc. More famous are BeanUtil and ModelMapper. They are simple to use, but they are unable to do so in slightly complex business scenarios. The MapStruct plug-in can be used to handle the attribute mapping between domin entity classes and model classes, and is highly configurable.
Create a Maven project
MapStruct requires support from eye-catching build tools (such as Maven). If the project structure is not standard, the corresponding conversion class may not be generated. Here I use Maven to build the project.
<properties> <>1.2.0.CR1</> </properties>
MapStruct is a tool that has been improving. The subsequent versions continue to improve the shortcomings of previous versions, fix bugs in previous versions, and the best stable version when using them.
<dependencies> <dependency> <groupId></groupId> <artifactId>mapstruct-jdk8</artifactId> <version>${}</version> </dependency> </dependencies>
For dependency packages that need to be introduced, you can see that Java 8 is used, and the latest version even supports Java 9. Of course, this doesn't think you have to use java8, and it's OK to use java version higher than java6.
<build> <plugins> <plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <annotationProcessorPaths> <path> <groupId></groupId> <artifactId>mapstruct-processor</artifactId> <version>${}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>
Maven plug-in is essential and the format is fixed.
Simple example
There is an entity class - User
public class User { private int id; private String name; private boolean married; // setters, getters, toString }
There is an entity class - Employee Employee, and the employee is also a user, but there is one more attribute than the user - Position position
public class Employee { private int id; private String name; private String position; private boolean married; // setters, getters, toString }
In specific business scenarios, the User object needs to be converted to Employee object, and sometimes the Employee object needs to be converted to User object.
If you use MapStrut, you need to write an interface:
@Mapper public interface UserMapper { UserMapper INSTANCE = (); Employee userToEmployee(User user); User employeeToUser(Employee employee); }
Running example
First of all,mvn compile
, automatically generate conversion code. The generated code is placed intarget/annotations
under. The general code is as follows:
import ; import ; import ; @Generated( value = "", date = "2017-08-26T17:08:23+0800", comments = "version: 1.2.0.CR1, compiler: javac, environment: Java 1.8.0_92 (Oracle Corporation)" ) public class UserMapperImpl implements UserMapper { @Override public Employee userToEmployee(User user) { if ( user == null ) { return null; } Employee employee = new Employee(); ( () ); ( () ); ( () ); return employee; } @Override public User employeeToUser(Employee employee) { if ( employee == null ) { return null; } User user = new User(); ( () ); ( () ); ( () ); return user; } }
Just write a test experiment.
public class AppTest{ @Test public void appTest(){ User user = new User(); (125); ("Chao"); (false); Employee e = (user); (e); (()); } }
Results output:
Employee [id=125, name=Chao, position=null, married=false]
The effect is good, and the fields that need to be converted are accurate, but some people are going to complain that this usage is much more complicated than BeanUtil, and it only achieves the same effect. This simple conversion does not require MapStruct, it does more complex business scenarios.
MapStruct issue
The conversion is inaccurate.
MapStruct has been updated, and some features cannot be recognized in old versions. When using them, you should use a relatively new version.
2. Under Eclipse, M2E plug-in support is required. If there is no integration in Eclipse, you need to go toEclipse Marketplace
Download and install, sometimes you need to specify the following configuration inproperties to enable Annotation Processing
<>jdt_apt</>
3. Under Eclipse, Maven compile promptsNothing to compile - all classes are up to date
, you can try to fill in it in Goldsclean install compile
。
When compiling, you must use JDK, you cannot use JRE, and the version must be higher than jdk6.
5. In Eclipse, it is confirmed that * has been generated, but an error was reported during runtime.ClassNotFoundException: Cannot find implementation for com...*Mapper
Right-click on the project -> properties -> Java Compiler -> Annotation Processing (Enable all items) -> Factory Path (Enable) -> Add External JARS -> Selectmapstruct-processor-*.jar
(Probably in the computer directory.m2\repository\org\mapstruct\mapstruct-processor
) – > OK
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links