Source: toutiao.com/i6891531055631696395

How often do you struggle to write the original code for entity conversion, especially if there are too many entity fields? Mapstruct is an open source project that can easily and elegantly transform and simplify your code.

Of course, some people like to write get sets, or copy them with BeanUtils. The code is just a tool, and this article is just an idea.

Let’s start with mapstruct.org/

No more nonsense, on the code:

Pom configuration:

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> Piler < maven.com. Source > 1.8 < / maven.com piler. Source > < maven.com piler. Target > 1.8 < / maven.com piler. Target > < org. Mapstruct. Version > 1.4.1. Final < / org. Mapstruct. Version > < org. Projectlombok. Version > 1.18.12 < / org. Projectlombok. Version > < / properties > < dependencies > < the dependency > <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>${org.mapstruct.version}</version> </dependency> <! -- lombok dependencies should not end up on classpath --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${org.projectlombok.version}</version> <scope>provided</scope> </dependency> <! -- IDEA 2018.1.1 need to add the following configuration, later versions do not need, can comment out, <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct </artifactId> <version>${org.mapstruct.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> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${org.projectlombok.version}</version> </path> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>${org.mapstruct.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build>Copy the code

Maven plugins are compatible with Lombok and Mapstruct versions. Maven plugins are compatible with Lombok versions 3.6.0 and 1.16.16. No property named “aaa” exists in source parameter(s). Did you mean “null”?

This exception is the result of a Lombok compilation exception that causes the lack of a get setter method. The absence of a constructor also throws an exception. Learning materials: Java advanced video resources

@Data @Builder @AllArgsConstructor @NoArgsConstructor public class Student { private String name; private int age; private GenderEnum gender; private Double height; private Date birthday; } public enum GenderEnum {Male("1", "Male "), Female("0"," Female "); private String code; private String name; public String getCode() { return this.code; } public String getName() { return this.name; } GenderEnum(String code, String name) { this.code = code; this.name = name; } } @Data @Builder @AllArgsConstructor @NoArgsConstructor public class StudentVO { private String name; private int age; private String gender; private Double height; private String birthday; } @Mapper public interface StudentMapper { StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class); @Mapping(source = "gender.name", target = "gender") @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss") StudentVO student2StudentVO(Student student); }Copy the code

Entity class is the development process is indispensable, even if it is generated by tools must also have, the need for handwriting is part of the Mapper interface, the completion of the compilation will automatically generate the corresponding implementation class

Then you can use mapper directly to convert entities

public class Test { public static void main(String[] args) { Student student = Student.builder().name("小 小明").age(6).gender(GenderEnum.Male).height(121.1).birthday(new Date()).build(); System.out.println(student); / / this line of code is practical to use StudentVO StudentVO = StudentMapper. INSTANCE. Student2StudentVO (student); System.out.println(studentVO); }}Copy the code

Mapper can map fields, change field types, specify formatting methods, and include default handling of some dates.

You can specify the formatting method manually:

@Mapper public interface StudentMapper { StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class); @Mapping(source = "gender", target = "gender") @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss") StudentVO student2StudentVO(Student student); default String getGenderName(GenderEnum gender) { return gender.getName(); }}Copy the code

The above is just the simplest handling of entity mapping. Here are some advanced uses

1. The List

Attribute mapping is based on the mapping configuration above

@Mapper public interface StudentMapper { StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class); @Mapping(source = "gender.name", target = "gender") @Mapping(source = "birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss") StudentVO student2StudentVO(Student student); List<StudentVO> students2StudentVOs(List<Student> studentList); } public static void main(String[] args) { Student student = Student.builder().name("小 小明").age(6).gender(GenderEnum.Male).height(121.1).birthday(new Date()).build(); List<Student> list = new ArrayList<>(); list.add(student); List<StudentVO> result = StudentMapper.INSTANCE.students2StudentVOs(list); System.out.println(result); }Copy the code

2. Convert multiple objects to one object

@Data @Builder @AllArgsConstructor @NoArgsConstructor public class Student { private String name; private int age; private GenderEnum gender; private Double height; private Date birthday; } @Data @AllArgsConstructor @Builder @NoArgsConstructor public class Course { private String courseName; private int sortNo; private long id; } @Data @Builder @AllArgsConstructor @NoArgsConstructor public class StudentVO { private String name; private int age; private String gender; private Double height; private String birthday; private String course; } @Mapper public interface StudentMapper { StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class); @Mapping(source = "student.gender.name", target = "gender") @Mapping(source = "student.birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss") @Mapping(source = "course.courseName", target = "course") StudentVO studentAndCourse2StudentVO(Student student, Course course); } public class Test { public static void main(String[] args) { Student student = Student.builder().name("小 小明").age(6).gender(GenderEnum.Male).height(121.1).birthday(new Date()).build(); Course = course.builder ().id(1L).build(); StudentVO studentVO = StudentMapper.INSTANCE.studentAndCourse2StudentVO(student, course); System.out.println(studentVO); }}Copy the code

3. The default value

@Mapper public interface StudentMapper { StudentMapper INSTANCE = Mappers.getMapper(StudentMapper.class); @Mapping(source = "student.gender.name", target = "gender") @Mapping(source = "student.birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss") @Mapping(source = "course.courseName", target = "course") @Mapping(target = "name", Source = "student. The name", defaultValue = "* *") StudentVO studentAndCourse2StudentVO (student student, Course Course); }Copy the code


Recommend 3 original Springboot +Vue projects, with complete video explanation and documentation and source code:

Build a complete project from Springboot+ ElasticSearch + Canal

  • Video tutorial: www.bilibili.com/video/BV1Jq…
  • A complete development documents: www.zhuawaba.com/post/124
  • Online demos: www.zhuawaba.com/dailyhub

【VueAdmin】 hand to hand teach you to develop SpringBoot+Jwt+Vue back-end separation management system

  • Full 800 – minute video tutorial: www.bilibili.com/video/BV1af…
  • Complete development document front end: www.zhuawaba.com/post/18
  • Full development documentation backend: www.zhuawaba.com/post/19

【VueBlog】 Based on SpringBoot+Vue development of the front and back end separation blog project complete teaching

  • Full 200 – minute video tutorial: www.bilibili.com/video/BV1af…
  • Full development documentation: www.zhuawaba.com/post/17

If you have any questions, please come to my official account [Java Q&A Society] and ask me