首页 >excel操作 > 内容

一款PO VO DTO 转换神器,求求你别在到处找工具类了!

2023年7月8日 11:24

点击上方蓝色字体,选择“标星公众号”

优质文章,第一时间送达


关注公众号后台回复pay或mall获取实战项目资料+视频

作者:bettermann

toutiao.com/i6891531055631696395/

老铁们是不是经常为写一些实体转换的原始代码感到头疼,尤其是实体字段特别多的时候。介绍一个开源项目 mapstruct ,可以轻松优雅的进行转换,简化你的代码。当然有的人喜欢写get set,或者用BeanUtils 进行复制,代码只是工具,本文只是提供一种思路。

先贴下官网地址吧:https://mapstruct.org/

废话不多说,上代码:

pom 配置:

<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><org.mapstruct.version>1.4.1.Final</org.mapstruct.version><org.projectlombok.version>1.18.12</org.projectlombok.version></properties><dependencies><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct</artifactId><version>${org.mapstruct.version}</version></dependency><!--lombokdependenciesshouldnotenduponclasspath--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${org.projectlombok.version}</version><scope>provided</scope></dependency><!--idea2018.1.1之前的版本需要添加下面的配置,后期的版本就不需要了,可以注释掉,我自己用的2019.3--><dependency><groupId>org.mapstruct</groupId><artifactId>mapstruct-processor</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><path><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>

关于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"?

这种异常就是lombok编译异常导致缺少get setter方法造成的。还有就是缺少构造函数也会抛异常。

@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();}}

上面只是最简单的实体映射处理,下面介绍一些高级用法

1.List 转换

属性映射基于上面的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);List<StudentVO>students2StudentVOs(List<Student>studentList);}publicstaticvoidmain(String[]args){Studentstudent=Student.builder().name("小明").age(6).gender(GenderEnum.Male).height(121.1).birthday(newDate()).build();List<Student>list=newArrayList<>();list.add(student);List<StudentVO>result=StudentMapper.INSTANCE.students2StudentVOs(list);System.out.println(result);}

2.多对象转换到一个对象

@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);}
有热门推荐????给IDEA换个酷炫的主题,这个有点哇塞啊!一条 SQL 引发的事故,同事直接被开除!!Mybatis 使用的 9 种设计模式,真是太有用了Java数组转List的三种方式及对比推荐一款日志切割神器!我常用~使用 Stream API 高逼格 优化 Java 代码!Spring MVC请求处理过程不是两张流程图就能讲清楚的撸一个简易聊天室,不信你学不会实时消息推送(附源码)Spring Boot 打包不同环境配置与 Shell 脚本部署最后分享一套微服务电商项目教程(资料笔记+视频):点击阅读全文获取面试资料+项目实战资料(电商/聚合支付)点击阅读原文,前往上面微服务电商教程文档页


参考文章:https://blog.csdn.net/qq_17231297/article/details/109590214

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时候联系我们修改或删除,在此表示感谢。

特别提醒:

1、请用户自行保存原始数据,为确保安全网站使用完即被永久销毁,如何人将无法再次获取。

2、如果上次文件较大或者涉及到复杂运算的数据,可能需要一定的时间,请耐心等待一会。

3、请按照用户协议文明上网,如果发现用户存在恶意行为,包括但不限于发布不合适言论妄图

     获取用户隐私信息等行为,网站将根据掌握的情况对用户进行限制部分行为、永久封号等处罚。

4、如果文件下载失败可能是弹出窗口被浏览器拦截,点击允许弹出即可,一般在网址栏位置设置

5、欢迎将网站推荐给其他人,网站持续更新更多功能敬请期待,收藏网站高效办公不迷路。

      



登录后回复

共有0条评论