发布时间:2021-09-20 13:37:29来源:ImportNew
先贴下官网地址吧:https://mapstruct.org/
废话不多说,上代码:
pom配置:
这种异常就是lombok编译异常导致缺少getsetter方法造成的。还有就是缺少构造函数也会抛异常。
@Data@Builder@AllArgsConstructor@NoArgsConstructorpublicclassStudent{privateStringname;privateintage;privateGenderEnumgender;privateDoubleheight;privateDatebirthday;}publicenumGenderEnum{Male("1","男"),Female("0","女");privateStringcode;privateStringname;publicStringgetCode(){returnthis.code;}publicStringgetName(){returnthis.name;}GenderEnum(Stringcode,Stringname){this.code=code;this.name=name;}}@Data@Builder@AllArgsConstructor@NoArgsConstructorpublicclassStudentVO{privateStringname;privateintage;privateStringgender;privateDoubleheight;privateStringbirthday;}@MapperpublicinterfaceStudentMapper{StudentMapperINSTANCE=Mappers.getMapper(StudentMapper.class);@Mapping(source="gender.name",target="gender")@Mapping(source="birthday",target="birthday",dateFormat="yyyy-MM-ddHH:mm:ss")StudentVOstudent2StudentVO(Studentstudent);}实体类是开发过程少不了的,就算是用工具生成肯定也是要有的,需要手写的部分就是这个Mapper的接口,编译完成后会自动生成相应的实现类
然后就可以直接用mapper进行实体的转换了
publicclassTest{publicstaticvoidmain(String[]args){Studentstudent=Student.builder().name("小明").age(6).gender(GenderEnum.Male).height(121.1).birthday(newDate()).build();System.out.println(student);//这行代码便是实际要用的代码StudentVOstudentVO=StudentMapper.INSTANCE.student2StudentVO(student);System.out.println(studentVO);}}mapper可以进行字段映射,改变字段类型,指定格式化的方式,包括一些日期的默认处理。
可以手动指定格式化的方法:
@MapperpublicinterfaceStudentMapper{StudentMapperINSTANCE=Mappers.getMapper(StudentMapper.class);@Mapping(source="gender",target="gender")@Mapping(source="birthday",target="birthday",dateFormat="yyyy-MM-ddHH:mm:ss")StudentVOstudent2StudentVO(Studentstudent);defaultStringgetGenderName(GenderEnumgender){returngender.getName();}}上面只是最简单的实体映射处理,下面介绍一些高级用法
属性映射基于上面的mapping配置
@MapperpublicinterfaceStudentMapper{StudentMapperINSTANCE=Mappers.getMapper(StudentMapper.class);@Mapping(source="gender.name",target="gender")@Mapping(source="birthday",target="birthday",dateFormat="yyyy-MM-ddHH:mm:ss")StudentVOstudent2StudentVO(Studentstudent);List2.多对象转换到一个对象@Data@Builder@AllArgsConstructor@NoArgsConstructorpublicclassStudent{privateStringname;privateintage;privateGenderEnumgender;privateDoubleheight;privateDatebirthday;}@Data@AllArgsConstructor@Builder@NoArgsConstructorpublicclassCourse{privateStringcourseName;privateintsortNo;privatelongid;}@Data@Builder@AllArgsConstructor@NoArgsConstructorpublicclassStudentVO{privateStringname;privateintage;privateStringgender;privateDoubleheight;privateStringbirthday;privateStringcourse;}@MapperpublicinterfaceStudentMapper{StudentMapperINSTANCE=Mappers.getMapper(StudentMapper.class);@Mapping(source="student.gender.name",target="gender")@Mapping(source="student.birthday",target="birthday",dateFormat="yyyy-MM-ddHH:mm:ss")@Mapping(source="course.courseName",target="course")StudentVOstudentAndCourse2StudentVO(Studentstudent,Coursecourse);}publicclassTest{publicstaticvoidmain(String[]args){Studentstudent=Student.builder().name("小明").age(6).gender(GenderEnum.Male).height(121.1).birthday(newDate()).build();Coursecourse=Course.builder().id(1L).courseName("语文").build();StudentVOstudentVO=StudentMapper.INSTANCE.studentAndCourse2StudentVO(student,course);System.out.println(studentVO);}}
3.默认值@MapperpublicinterfaceStudentMapper{StudentMapperINSTANCE=Mappers.getMapper(StudentMapper.class);@Mapping(source="student.gender.name",target="gender")@Mapping(source="student.birthday",target="birthday",dateFormat="yyyy-MM-ddHH:mm:ss")@Mapping(source="course.courseName",target="course")@Mapping(target="name",source="student.name",defaultValue="张三")StudentVOstudentAndCourse2StudentVO(Studentstudent,Coursecourse)
转自:toutiao.com/i6891531055631696395
-EOF-
推荐阅读点击标题可跳转
1、
2、
3、
看完本文有收获?请转发分享给更多人
关注「ImportNew」,提升Java技能
点赞和在看就是最大的支持❤️