老鐵們是不是經常為寫一些實體轉換的原始代碼感到頭疼,尤其是實體字段特別多的時候。介紹一個開源項目 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文章
340瀏覽量
14360 -
開源項目
+關注
關注
0文章
38瀏覽量
7213
原文標題:別再用 BeanUtils 了,這款 PO VO DTO 轉換神器不香么?
文章出處:【微信號:芋道源碼,微信公眾號:芋道源碼】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論