老铁们是不是经常为写一些实体转换的原始代码感到头疼,尤其是实体字段特别多的时候。介绍一个开源项目 mapstruct ,可以轻松优雅的进行转换,简化你的代码。当然有的人喜欢写get set,或者用BeanUtils 进行复制,代码只是工具,本文只是提供一种思路。
先贴下官网地址吧:https://mapstruct.org/
废话不多说,上代码:
pom 配置:
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<org.mapstruct.version>1.4.1.Finalorg.mapstruct.version>
<org.projectlombok.version>1.18.12org.projectlombok.version>
properties>
<dependencies>
<dependency>
<groupId>org.mapstructgroupId>
<artifactId>mapstructartifactId>
<version>${org.mapstruct.version}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${org.projectlombok.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.mapstructgroupId>
<artifactId>mapstruct-processorartifactId>
<version>${org.mapstruct.version}version>
<scope>providedscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
<configuration>
<source>1.8source>
<target>1.8target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${org.projectlombok.version}version>
path>
<path>
<groupId>org.mapstructgroupId>
<artifactId>mapstruct-processorartifactId>
<version>${org.mapstruct.version}version>
path>
annotationProcessorPaths>
configuration>
plugin>
plugins>
build>
关于lombok和mapstruct的版本兼容问题多说几句,maven插件要使用3.6.0版本以上、lombok使用1.16.16版本以上,另外编译的lombok mapstruct的插件不要忘了加上。否则会出现下面的错误:No property named "aaa" exists in source parameter(s). Did you mean "null"?
基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
- 项目地址:https://github.com/YunaiV/ruoyi-vue-pro
- 视频教程:https://doc.iocoder.cn/video/
这种异常就是lombok编译异常导致缺少get setter方法造成的。还有就是缺少构造函数也会抛异常。
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
publicclassStudent{
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
@NoArgsConstructor
publicclassStudentVO{
privateStringname;
privateintage;
privateStringgender;
privateDoubleheight;
privateStringbirthday;
}
@Mapper
publicinterfaceStudentMapper{
StudentMapperINSTANCE=Mappers.getMapper(StudentMapper.class);
@Mapping(source="gender.name",target="gender")
@Mapping(source="birthday",target="birthday",dateFormat="yyyy-MM-ddHHss")
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可以进行字段映射,改变字段类型,指定格式化的方式,包括一些日期的默认处理。
可以手动指定格式化的方法:
@Mapper
publicinterfaceStudentMapper{
StudentMapperINSTANCE=Mappers.getMapper(StudentMapper.class);
@Mapping(source="gender",target="gender")
@Mapping(source="birthday",target="birthday",dateFormat="yyyy-MM-ddHHss")
StudentVOstudent2StudentVO(Studentstudent);
defaultStringgetGenderName(GenderEnumgender){
returngender.getName();
}
}
上面只是最简单的实体映射处理,下面介绍一些高级用法
1、List 转换
属性映射基于上面的mapping配置
@Mapper
publicinterfaceStudentMapper{
StudentMapperINSTANCE=Mappers.getMapper(StudentMapper.class);
@Mapping(source="gender.name",target="gender")
@Mapping(source="birthday",target="birthday",dateFormat="yyyy-MM-ddHHss")
StudentVOstudent2StudentVO(Studentstudent);
Liststudents2StudentVOs(ListstudentList) ;
}
publicstaticvoidmain(String[]args){
Studentstudent=Student.builder().name("小明").age(6).gender(GenderEnum.Male).height(121.1).birthday(newDate()).build();
Listlist=newArrayList<>();
list.add(student);
Listresult=StudentMapper.INSTANCE.students2StudentVOs(list);
System.out.println(result);
}
基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
2、多对象转换到一个对象
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
publicclassStudent{
privateStringname;
privateintage;
privateGenderEnumgender;
privateDoubleheight;
privateDatebirthday;
}
@Data
@AllArgsConstructor
@Builder
@NoArgsConstructor
publicclassCourse{
privateStringcourseName;
privateintsortNo;
privatelongid;
}
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
publicclassStudentVO{
privateStringname;
privateintage;
privateStringgender;
privateDoubleheight;
privateStringbirthday;
privateStringcourse;
}
@Mapper
publicinterfaceStudentMapper{
StudentMapperINSTANCE=Mappers.getMapper(StudentMapper.class);
@Mapping(source="student.gender.name",target="gender")
@Mapping(source="student.birthday",target="birthday",dateFormat="yyyy-MM-ddHHss")
@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);
}
}
基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
- 项目地址:https://gitee.com/zhijiantianya/yudao-cloud
- 视频教程:https://doc.iocoder.cn/video/
3、默认值
@Mapper
publicinterfaceStudentMapper{
StudentMapperINSTANCE=Mappers.getMapper(StudentMapper.class);
@Mapping(source="student.gender.name",target="gender")
@Mapping(source="student.birthday",target="birthday",dateFormat="yyyy-MM-ddHHss")
@Mapping(source="course.courseName",target="course")
@Mapping(target="name",source="student.name",defaultValue="张三")
StudentVOstudentAndCourse2StudentVO(Studentstudent,Coursecourse);
}
-
spring
+关注
关注
0文章
338浏览量
14301 -
开源项目
+关注
关注
0文章
36浏览量
7171
原文标题:别再用 BeanUtils 了,这款 PO VO DTO 转换神器不香么?
文章出处:【微信号:芋道源码,微信公众号:芋道源码】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
评论