<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          一款 PO VO DTO 轉(zhuǎn)換神器,求求你別在到處找工具類了!

          共 6580字,需瀏覽 14分鐘

           ·

          2020-11-13 19:43

          來源:toutiao.com/i6891531055631696395/


          老鐵們是不是經(jīng)常為寫一些實(shí)體轉(zhuǎn)換的原始代碼感到頭疼,尤其是實(shí)體字段特別多的時(shí)候。介紹一個(gè)開源項(xiàng)目 mapstruct ,可以輕松優(yōu)雅的進(jìn)行轉(zhuǎn)換,簡(jiǎn)化你的代碼。當(dāng)然有的人喜歡寫get set,或者用BeanUtils 進(jìn)行復(fù)制,代碼只是工具,本文只是提供一種思路。

          先貼下官網(wǎng)地址吧: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>

          關(guān)于lombok和mapstruct的版本兼容問題多說幾句,maven插件要使用3.6.0版本以上、lombok使用1.16.16版本以上,另外編譯的lombok mapstruct的插件不要忘了加上。否則會(huì)出現(xiàn)下面的錯(cuò)誤:No property named "aaa" exists in source parameter(s). Did you mean "null"?

          這種異常就是lombok編譯異常導(dǎo)致缺少get setter方法造成的。還有就是缺少構(gòu)造函數(shù)也會(huì)拋異常。

          @Data
          @Builder
          @AllArgsConstructor
          @NoArgsConstructor
          public?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
          @NoArgsConstructor
          public?class?StudentVO?{
          ????private?String?name;
          ????private?int?age;
          ????private?String?gender;
          ????private?Double?height;
          ????private?String?birthday;
          }
          @Mapper
          public?interface?StudentMapper?{

          ????StudentMapper?INSTANCE?=?Mappers.getMapper(StudentMapper.class);

          ????@Mapping(source?=?"gender.name",?target?=?"gender")
          ????@Mapping(source?=?"birthday",?target?=?"birthday",?dateFormat?=?"yyyy-MM-dd?HH:mm:ss")
          ????StudentVO?student2StudentVO(Student?student);

          }

          實(shí)體類是開發(fā)過程少不了的,就算是用工具生成肯定也是要有的,需要手寫的部分就是這個(gè)Mapper的接口,編譯完成后會(huì)自動(dòng)生成相應(yīng)的實(shí)現(xiàn)類

          然后就可以直接用mapper進(jìn)行實(shí)體的轉(zhuǎn)換了

          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);
          ????????//這行代碼便是實(shí)際要用的代碼
          ????????StudentVO?studentVO?=?StudentMapper.INSTANCE.student2StudentVO(student);
          ????????System.out.println(studentVO);

          ????}

          }

          mapper可以進(jìn)行字段映射,改變字段類型,指定格式化的方式,包括一些日期的默認(rèn)處理。

          可以手動(dòng)指定格式化的方法:

          @Mapper
          public?interface?StudentMapper?{

          ????StudentMapper?INSTANCE?=?Mappers.getMapper(StudentMapper.class);

          ????@Mapping(source?=?"gender",?target?=?"gender")
          ????@Mapping(source?=?"birthday",?target?=?"birthday",?dateFormat?=?"yyyy-MM-dd?HH:mm:ss")
          ????StudentVO?student2StudentVO(Student?student);

          ????default?String?getGenderName(GenderEnum?gender)?{
          ????????return?gender.getName();
          ????}

          }

          上面只是最簡(jiǎn)單的實(shí)體映射處理,下面介紹一些高級(jí)用法

          1、List 轉(zhuǎn)換

          屬性映射基于上面的mapping配置

          @Mapper
          public?interface?StudentMapper?{

          ????StudentMapper?INSTANCE?=?Mappers.getMapper(StudentMapper.class);

          ????@Mapping(source?=?"gender.name",?target?=?"gender")
          ????@Mapping(source?=?"birthday",?target?=?"birthday",?dateFormat?=?"yyyy-MM-dd?HH:mm:ss")
          ????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);
          }

          2、多對(duì)象轉(zhuǎn)換到一個(gè)對(duì)象

          @Data
          @Builder
          @AllArgsConstructor
          @NoArgsConstructor
          public?class?Student?{

          ????private?String?name;
          ????private?int?age;
          ????private?GenderEnum?gender;
          ????private?Double?height;
          ????private?Date?birthday;

          }
          @Data
          @AllArgsConstructor
          @Builder
          @NoArgsConstructor
          public?class?Course?{

          ????private?String?courseName;
          ????private?int?sortNo;
          ????private?long?id;

          }
          @Data
          @Builder
          @AllArgsConstructor
          @NoArgsConstructor
          public?class?StudentVO?{
          ????private?String?name;
          ????private?int?age;
          ????private?String?gender;
          ????private?Double?height;
          ????private?String?birthday;
          ????private?String?course;
          }
          @Mapper
          public?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?HH:mm:ss")
          ????@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);
          ????}

          }

          3、默認(rèn)值

          @Mapper
          public?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?HH:mm:ss")
          ????@Mapping(source?=?"course.courseName",?target?=?"course")
          ????@Mapping(target?=?"name",?source?=?"student.name",?defaultValue?=?"張三")
          ????StudentVO?studentAndCourse2StudentVO(Student?student,?Course?course);

          }


          題外話: 目前小哈正在個(gè)人博客(新搭建的網(wǎng)站,域名就是犬小哈的拼音)?www.quanxiaoha.com?上更新《Go語言教程》,畢竟Go自帶天然的并發(fā)優(yōu)勢(shì),后端的同學(xué)還是要學(xué)一下的,這個(gè)教程系列小哈會(huì)一直更新下去,目前已經(jīng)更新到 Go語言的基礎(chǔ)語法了,歡迎小伙伴們?cè)L問哦~

          END


          有熱門推薦?

          1.?賊厲害,手?jǐn)]的 SpringBoot 緩存系統(tǒng),性能杠杠的!

          2.?給 IDEA 換個(gè)酷炫的主題,這個(gè)有點(diǎn)哇塞??!

          3.?SpringCloud 之 Zuul 網(wǎng)關(guān)搭建及配置

          4.?2020 年 11 月編程語言排行榜,Python 超越 Java ?

          最近面試BAT,整理一份面試資料Java面試BATJ通關(guān)手冊(cè),覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫(kù)、數(shù)據(jù)結(jié)構(gòu)等等。

          獲取方式:點(diǎn)“在看”,關(guān)注公眾號(hào)并回復(fù)?Java?領(lǐng)取,更多內(nèi)容陸續(xù)奉上。

          文章有幫助的話,在看,轉(zhuǎn)發(fā)吧。

          謝謝支持喲 (*^__^*)

          瀏覽 21
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  91色情片| 久久三级电影网站 | 日韩中文字幕视频在线 | 爽灬爽灬爽灬高潮无码视频直播 | 网站黄色国产 |