老铁们是不是经常为写一些实体转换的原始代码感到头疼,尤其是实体字段特别多的时候。介绍一个开源项目 mapstruct ,可以轻松优雅的进行转换,简化你的代码。当然有的人喜欢写get set,或者用beanutils 进行复制,代码只是工具,本文只是提供一种思路。
先贴下官网地址吧:https://mapstruct.org/
废话不多说,上代码:
pom 配置:
utf-8 1.8 1.8 1.4.1.final 1.18.12 org.mapstruct mapstruct ${org.mapstruct.version} org.projectlombok lombok ${org.projectlombok.version} provided org.mapstruct mapstruct-processor ${org.mapstruct.version} provided org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 org.projectlombok lombok ${org.projectlombok.version} org.mapstruct mapstruct-processor ${org.mapstruct.version} 关于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@noargsconstructorpublic class student { private string name; private int age; private genderenum gender; private double height; private date birthday;}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; }}@data@builder@allargsconstructor@noargsconstructorpublic class studentvo { private string name; private int age; private string gender; private double height; private string birthday;}@mapperpublic interface studentmapper { studentmapper instance = mappers.getmapper(studentmapper.class); @mapping(source = gender.name, target = gender) @mapping(source = birthday, target = birthday, dateformat = yyyy-mm-dd hhss) studentvo student2studentvo(student student);} 实体类是开发过程少不了的,就算是用工具生成肯定也是要有的,需要手写的部分就是这个mapper的接口,编译完成后会自动生成相应的实现类
然后就可以直接用mapper进行实体的转换了
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); //这行代码便是实际要用的代码 studentvo studentvo = studentmapper.instance.student2studentvo(student); system.out.println(studentvo); }} mapper可以进行字段映射,改变字段类型,指定格式化的方式,包括一些日期的默认处理。
可以手动指定格式化的方法:
@mapperpublic interface studentmapper { studentmapper instance = mappers.getmapper(studentmapper.class); @mapping(source = gender, target = gender) @mapping(source = birthday, target = birthday, dateformat = yyyy-mm-dd hhss) studentvo student2studentvo(student student); default string getgendername(genderenum gender) { return gender.getname(); }} 上面只是最简单的实体映射处理,下面介绍一些高级用法
1、list 转换 属性映射基于上面的mapping配置
@mapperpublic interface studentmapper { studentmapper instance = mappers.getmapper(studentmapper.class); @mapping(source = gender.name, target = gender) @mapping(source = birthday, target = birthday, dateformat = yyyy-mm-dd hhss) studentvo student2studentvo(student student); list students2studentvos(list 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 list = new arraylist(); list.add(student); list result = studentmapper.instance.students2studentvos(list); system.out.println(result);} 基于 spring cloud alibaba + gateway + nacos + rocketmq + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://github.com/yunaiv/yudao-cloud 视频教程:https://doc.iocoder.cn/video/ 2、多对象转换到一个对象 @data@builder@allargsconstructor@noargsconstructorpublic class student { private string name; private int age; private genderenum gender; private double height; private date birthday;}@data@allargsconstructor@builder@noargsconstructorpublic class course { private string coursename; private int sortno; private long id;}@data@builder@allargsconstructor@noargsconstructorpublic class studentvo { private string name; private int age; private string gender; private double height; private string birthday; private string course;}@mapperpublic 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 hhss) @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 = course.builder().id(1l).coursename(语文).build(); studentvo studentvo = 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、默认值 @mapperpublic 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 hhss) @mapping(source = course.coursename, target = course) @mapping(target = name, source = student.name, defaultvalue = 张三) studentvo studentandcourse2studentvo(student student, course course);}
IGBT功率模块散热基板的作用及种类 车规级IGBT功率模块的散热方式
FinFET技术未来有怎样的发展的前景
海速芯8位MCU-TM52F1376用于电动剃须刀
通讯信号隔断器一般都应用在什么地方
电动机绕组崩烧的原因
别再用BeanUtils了,这款PO VO DTO转换神器不香么?
集成运放电路设计原理图
压摆率是什么意思
国务院的工作报告明确提出了要推动工业互联网和“智能+”
厂界VOC气象站技术参数是什么
WiFi芯片企业尊湃通讯宣布Pre-A轮融资超募3亿元
arm技术中厚膜电路设计软件
多生理参数的无线实时监护系统设计
帮助我们创建数据科学领域的下一代数据可视化工具
印制电路板金属表面的预备处理技术
预装macOS和win10,又一家公司违法销售“黑苹果”电脑
2018年一季度智能手机出货量约3.2亿部,同比下降8%
宠物芯片采用物联网射频识别技术,实现系统登陆100%“有芯”
人工智能在未来10年真的能取代人类50%的工作吗
基于ARM9的船载监测系统设计