在一个成熟的工程中,尤其是现在的分布式系统中,应用与应用之间,还有单独的应用细分模块之后,DO 一般不会让外部依赖,这时候需要在提供对外接口的模块里放 DTO 用于对象传输,也即是 DO 对象对内,DTO对象对外,DTO 可以根据业务需要变更,并不需要映射 DO 的全部属性;这种对象与对象之间的互相转换,就需要有一个专门用来解决转换问题的工具,毕竟每一个字段都 get/set 会很麻烦。
第一步 :引入依赖
<properties>
<mapstruct.version>1.3.1.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-jdk8</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</dependency>
</dependencies>
第二步:创建mapper接口
加入
DemoMapper INSTANCE = Mappers.getMapper(DemoMapper.class);
如果属性名都可以对上的话 那么就没必要使用@mapping 如果要将某个值赋给某个的话 那么就需要使用。
下边的实体类只是演示
@Mapper
public interface DemoMapper {
DemoMapper INSTANCE = Mappers.getMapper(DemoMapper.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);
@Mapping(source = "student.gender.name", target = "gender")
@Mapping(source = "student.birthday", target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
@Mapping(source = "course.id", target = "id")
@Mapping(source = "course.courseName", target = "course")
@Mapping(source = "aaalist", target = "studentList")
StudentVO studentAndCourse2StudentVO(Student student, Course course, List<Student> aaalist);
@Data
public class Student {
private long id;
private String name;
private int age;
private GenderEnum gender;
private Double height;
private Date birthday;
}
@Data
public class Course {
private String courseName;
private int sortNo;
private long id;
}
@Data
public class StudentVO {
private long id;
private String name;
private int age;
private String gender;
private Double height;
private String birthday;
private String course;
private List<StudentVO> studentList;
}
public enum GenderEnum {
Male("1", "男"),
Female("0", "女");
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;
}
}
}
如何使用--------------------------------------------------------------------------------------------------------------------
public class DemoTest {
public /*static*/ void main(String[] args) {
DemoMapper.Student student = new DemoMapper.Student();
student.setId(11111);
student.setName("张三");
student.setAge(23);
student.setBirthday(new Date());
student.setGender(DemoMapper.GenderEnum.Male);
student.setHeight(160.5);
DemoMapper.Course course = new DemoMapper.Course();
course.setId(22222);
course.setCourseName("语文");
course.setSortNo(3);
// 下边一句就是在进行转换
DemoMapper.StudentVO studentVO1 = DemoMapper.INSTANCE.student2StudentVO(student);
System.out.println(studentVO1);
List<DemoMapper.Student> list = new ArrayList<>();
list.add(student);
// 下边一句就是在进行转换
List<DemoMapper.StudentVO> result = DemoMapper.INSTANCE.students2StudentVOs(list);
System.out.println(result);
// 下边一句就是在进行转换
DemoMapper.StudentVO studentVO2 = DemoMapper.INSTANCE.studentAndCourse2StudentVO(student, course, list);
System.out.println(studentVO2);
}
}
工作笔记 仅供参考 。。。。。。。。。。 如有问题请进行留言。