91欧美超碰AV自拍|国产成年人性爱视频免费看|亚洲 日韩 欧美一厂二区入|人人看人人爽人人操aV|丝袜美腿视频一区二区在线看|人人操人人爽人人爱|婷婷五月天超碰|97色色欧美亚州A√|另类A√无码精品一级av|欧美特级日韩特级

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

PO VO DTO轉(zhuǎn)換神器的思路

Linux愛好者 ? 來源:今日頭條 ? 作者:bettermann ? 2021-10-12 11:13 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

當(dāng)然有的人喜歡寫get set,或者用BeanUtils 進行復(fù)制,代碼只是工具,本文只是提供一種思路。

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的插件不要忘了加上。否則會出現(xiàn)下面的錯誤:No property named "aaa" exists in source parameter(s). Did you mean "null"?

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

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

}

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

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

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 轉(zhuǎn)換

屬性映射基于上面的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);
}

2.多對象轉(zhuǎn)換到一個對象

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

}

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);

}

	

		

轉(zhuǎn)自:toutiao.com/i6891531055631696395

責(zé)任編輯:haq
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 轉(zhuǎn)換器
    +關(guān)注

    關(guān)注

    27

    文章

    9420

    瀏覽量

    156418
  • 代碼
    +關(guān)注

    關(guān)注

    30

    文章

    4968

    瀏覽量

    74013
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關(guān)推薦
    熱點推薦

    LTC3330:納米功耗的能量采集與電池續(xù)航神器

    LTC3330:納米功耗的能量采集與電池續(xù)航神器 在電子設(shè)備的設(shè)計中,如何高效地利用能量并延長電池壽命一直是工程師們關(guān)注的焦點。今天,我們將深入探討 Linear Technology 公司
    的頭像 發(fā)表于 03-10 10:15 ?43次閱讀

    疆鴻智能讓PROFINET與CANopen無縫對話:揭秘智慧物流的“網(wǎng)關(guān)”神器

    疆鴻智能讓PROFINET與CANopen無縫對話:揭秘智慧物流的“網(wǎng)關(guān)”神器 隨著工業(yè)4.0和智能制造的深入推進,工業(yè)現(xiàn)場總線技術(shù)正在經(jīng)歷一場深刻的變革。在復(fù)雜的自動化產(chǎn)線中,沒有任何一種通訊協(xié)議
    的頭像 發(fā)表于 02-24 15:49 ?124次閱讀
    疆鴻智能讓PROFINET與CANopen無縫對話:揭秘智慧物流的“網(wǎng)關(guān)”<b class='flag-5'>神器</b>

    5大理由:聲學(xué)工程師為何依賴 GRAS 40PO 系列實現(xiàn)穩(wěn)定、可擴展的測試

    當(dāng)聲學(xué)驗證需要兼顧可重復(fù)性與測量速度時,測量鏈中任何微小的低效環(huán)節(jié)都可能產(chǎn)生疊加影響。無論您正在驗證移動設(shè)備、音頻電子設(shè)備還是聲學(xué)傳感器,傳聲器都不應(yīng)成為變量因素。GRAS40PO-L和40PO
    的頭像 發(fā)表于 11-17 09:04 ?691次閱讀
    5大理由:聲學(xué)工程師為何依賴 GRAS 40<b class='flag-5'>PO</b> 系列實現(xiàn)穩(wěn)定、可擴展的測試

    CW32的ADC視線,DMA擴展采樣思路

    如果需要對超過 4 路的模擬量進行采樣,則需要結(jié)合 DMA 的功能,以實現(xiàn)較少的 CPU 參與。其思路如下: 1.ADC 配置為單通道單次轉(zhuǎn)換,完成轉(zhuǎn)換后硬件觸發(fā) DMA; 2.DMA
    發(fā)表于 11-13 08:09

    橢偏光譜技術(shù)在VO?薄膜光誘導(dǎo)IMT中的應(yīng)用:瞬態(tài)介電函數(shù)的動力學(xué)路徑解析

    二氧化釩(VO?)作為一種強關(guān)聯(lián)電子材料,在約68°C時會發(fā)生絕緣體-金屬相變(IMT),并伴隨晶體結(jié)構(gòu)變化,這一現(xiàn)象使其在超快光子器件(如光開關(guān)和調(diào)制器)中具有巨大應(yīng)用潛力。然而,要實現(xiàn)對其光誘導(dǎo)
    的頭像 發(fā)表于 11-12 18:02 ?498次閱讀
    橢偏光譜技術(shù)在<b class='flag-5'>VO</b>?薄膜光誘導(dǎo)IMT中的應(yīng)用:瞬態(tài)介電函數(shù)的動力學(xué)路徑解析

    Vishay VO1401AEF固態(tài)繼電器技術(shù)深度解析

    通電阻和 3750V~RMS~ 隔離測試電壓。VO1401AEF固態(tài)繼電器提供無抖動的開關(guān)操作,并配有與TTL/CMOS兼容的輸入。該繼電器用于安防系統(tǒng)、儀器儀表和工業(yè)控制應(yīng)用。
    的頭像 發(fā)表于 11-10 15:43 ?651次閱讀
    Vishay <b class='flag-5'>VO</b>1401AEF固態(tài)繼電器技術(shù)深度解析

    協(xié)議轉(zhuǎn)換神器!Profinet轉(zhuǎn)Devicenet秒通,數(shù)據(jù)無縫集成

    在現(xiàn)代工業(yè)自動化領(lǐng)域,不同廠商的設(shè)備往往采用不同的通信協(xié)議,導(dǎo)致系統(tǒng)集成面臨挑戰(zhàn)。尤其在新材料生產(chǎn)領(lǐng)域,如不銹鋼寬板冷軋生產(chǎn)線,高精度、高實時性的通信需求使得協(xié)議轉(zhuǎn)換成為關(guān)鍵環(huán)節(jié)。其中,將
    的頭像 發(fā)表于 09-11 16:18 ?491次閱讀
    協(xié)議<b class='flag-5'>轉(zhuǎn)換</b><b class='flag-5'>神器</b>!Profinet轉(zhuǎn)Devicenet秒通,數(shù)據(jù)無縫集成

    【精選直播】無感FOC控制中滑模觀測器估算轉(zhuǎn)子角度思路分享

    直播預(yù)告掃碼購買課程&預(yù)約直播直播亮點1、FOC無感控制框圖分析2、電機數(shù)學(xué)模型回顧3、轉(zhuǎn)子位置角求取思路4、滑模觀測器思路分享5、滑模觀測器的實現(xiàn)直播大綱1、無感FOC控制框圖分析2、電機
    的頭像 發(fā)表于 08-05 08:06 ?1166次閱讀
    【精選直播】無感FOC控制中滑模觀測器估算轉(zhuǎn)子角度<b class='flag-5'>思路</b>分享

    開關(guān)電源維修思路及常見故障

    開關(guān)電源的維修思路及常見故障處理是電子技術(shù)人員需要掌握的重要技能。以下是對開關(guān)電源維修思路及常見故障的詳細分析。 ? 一、開關(guān)電源維修思路 1. 斷電檢查: ? ? ● ?外觀檢查:打開電源的外殼
    的頭像 發(fā)表于 08-03 07:38 ?2512次閱讀

    流量計連接神器 CClinkie轉(zhuǎn)Modbus RTU:工程師的「斷舍離」指南

    不必要的成本。有沒有一種\"斷舍離\"的方法,讓工程師擺脫這個煩惱?答案就是耐達訊通信技術(shù)CClinkie轉(zhuǎn)Modbus RTU的轉(zhuǎn)換方案。 方案概述: CClinkie轉(zhuǎn)
    發(fā)表于 06-24 13:53

    (ST大賽三等獎作品)超聲波自拍神器實例項目

    (ST大賽三等獎作品)超聲波自拍神器電路圖:
    發(fā)表于 05-28 21:04

    CCLINKIE轉(zhuǎn)PROFINET:電機的“網(wǎng)絡(luò)沖浪神器”!

    PROFINET的大家庭。有了它,電機就像裝上了“智能小馬達”,和其他設(shè)備的配合那叫一個默契,生產(chǎn)效率直接“起飛”! 在這里,我必須給大家推薦一款“神器”——耐達訊NY-N831 -CCLINKIE網(wǎng)關(guān)。這
    發(fā)表于 05-28 15:21

    全屋燈光秒變聰明,這個提升幸福感的神器你還沒安排嗎?

    全屋燈光秒變聰明這個提升幸福感的神器你還沒安排嗎?我寶子們,你是否受夠了摸黑找開關(guān)的狼狽、手動調(diào)光的繁瑣,或是永遠調(diào)不出理想氛圍的無奈?作為專注智能燈控方案的我們,今天就來揭秘——如何讓家里的燈光
    的頭像 發(fā)表于 05-14 18:15 ?1295次閱讀
    全屋燈光秒變聰明,這個提升幸福感的<b class='flag-5'>神器</b>你還沒安排嗎?

    計算機網(wǎng)絡(luò)排錯思路總結(jié)

    明人不說暗話,這篇文章我們來聊一個非常有用,同時也是程序員必備的技能,那就是網(wǎng)絡(luò)排錯思路大總結(jié)。
    的頭像 發(fā)表于 04-01 17:32 ?902次閱讀
    計算機網(wǎng)絡(luò)排錯<b class='flag-5'>思路</b>總結(jié)

    使用nonai_2d的CRC功能進行圖像類型轉(zhuǎn)換,nonai_2d模塊的要如何使用?

    我希望使用nonai_2d的CRC功能進行圖像類型轉(zhuǎn)換,參考sample_mcm例程添加了對應(yīng)的start和exit,并且做了sys_bind,也申請了vb,但是運行之后系統(tǒng)一直提示類似沒有緩沖區(qū)
    發(fā)表于 03-11 06:44