1, the preface
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. XML 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>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.
@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 entity mapping. Here are some advanced uses:
2. List conversion
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
3. 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. 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
PS: In case you can’t find this article, please click “like” to browse and find it.