在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

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

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

3天內不再提示

PO VO DTO轉換神器的思路

Linux愛好者 ? 來源:今日頭條 ? 作者:bettermann ? 2021-10-12 11:13 ? 次閱讀

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

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

這種異常就是lombok編譯異常導致缺少get setter方法造成的。還有就是缺少構造函數(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的接口,編譯完成后會自動生成相應的實現(xiàn)類

然后就可以直接用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);
}

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

}

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

}

	

		

轉自:toutiao.com/i6891531055631696395

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

    關注

    27

    文章

    8746

    瀏覽量

    148086
  • 代碼
    +關注

    關注

    30

    文章

    4829

    瀏覽量

    69071
收藏 人收藏

    評論

    相關推薦

    TI_SN74LVC2G125DCUR為什么會有兩個Vo輸出,每個都代表什么意思?

    如上圖紅色圓圈部分,為什么會有兩個Vo輸出,每個都代表什么意思?以及為什么沒有在手冊中看到關于輸入阻抗?
    發(fā)表于 01-07 08:15

    BUCK電路占空比對轉換效率的影響

    周期T_SW的比例,計算公式為D=T_ON/T_SW。在BUCK電路中,占空比D還可以根據(jù)伏秒積平衡原理計算,即VIN*TON=VO*TOFF,從而得出D=Vo/Vin(在忽略開關管和電感等元件的壓降時)。這里的Vo是輸出電壓,
    的頭像 發(fā)表于 12-12 15:39 ?1129次閱讀

    用TPA2005D1搭了一個音頻放大電路,在測輸出端Vo+和Vo-的波形時,發(fā)現(xiàn)有雜波,怎么去除?

    最近用TPA2005D1搭了一個音頻放大電路,試了一下,功能可以了,但是在測輸出端Vo+和Vo-的波形時,發(fā)現(xiàn)有雜波,電路圖和波形圖在下面,請知道的大神幫忙分析一下,怎么去雜波??
    發(fā)表于 10-21 08:11

    監(jiān)控平臺設計思路

    電子發(fā)燒友網(wǎng)站提供《監(jiān)控平臺設計思路.pptx》資料免費下載
    發(fā)表于 10-09 11:18 ?0次下載

    用OPA27做同相加法器Vo=Vi1+Vi2時遇到的疑問求解

    用OPA27做同相加法器Vo=Vi1+Vi2的時候,函數(shù)發(fā)生器給Vi1輸入頻率為1kHZ,Vpp=1V的正弦波,而Vi2接地時,沒有輸出。然后稍作改動,成了Vo=-(Vi1+Vi2),如下圖所示
    發(fā)表于 09-12 06:31

    PGA204的1管腳和9管腳沒有用到(Vo1,Vo2),布線時應該如何布線?

    PGA204的1管腳和9管腳沒有用到(Vo1,Vo2),布線時應該如何布線,是直接懸空還還是接AGND或者是VCC呢?
    發(fā)表于 08-28 08:20

    為什么LM324在測試失調電壓和失調電流時要Vo=1.4V,有什么依據(jù)嗎?

    為什么LM324在測試失調電壓和失調電流時要Vo=1.4V,有什么依據(jù)嗎
    發(fā)表于 08-02 09:32

    THS4509運放給+5V單電源供電,請問輸出電壓Vo+ - Vo-最大能輸出多少V?

    你好,請問這款運放給+5V單電源供電,請問輸出電壓Vo+ - Vo-最大能輸出多少V?
    發(fā)表于 08-01 08:16

    反激變換器DCM模式變壓器設計及元件選擇

    設計參數(shù)包括輸入電壓Vin(AC),輸出電壓Vo、輸出功率Po,效率h、開關頻率fs、最大占空比Dmax、工作模式和電流紋波系數(shù)K。非連續(xù)(斷續(xù))導通模式(DCM),具有更高穩(wěn)定性和更高效益,最大
    的頭像 發(fā)表于 07-02 14:58 ?2783次閱讀
    反激變換器DCM模式變壓器設計及元件選擇

    4G5G手機信號屏蔽器:手機信號“靜音”神器

    深圳特信電子|4G5G手機信號屏蔽器:手機信號“靜音”神器
    的頭像 發(fā)表于 07-02 08:51 ?686次閱讀

    請教延時電路的設計思路

    想設計一套延時啟動電路. 請老師們提供下思路 使用機械定時器, (電風扇的定時器) , 當定時結束時, 電機開始工作. 望賜教. 謝謝
    發(fā)表于 06-25 22:15

    RK3588 在使用rockit接口設置vo時,UI消失,大家遇到過嗎?

    使用RK3588,先通過vo顯示出來了UI,然后希望通過vo輸出解碼的視頻,在設置vo時,在調用RK_MPI_VO_GetPubAttr接口,UI直接消失,所在進程依然運行
    發(fā)表于 06-11 17:59

    LabVIEW控制單列表滾動條

    最近寫程序用到一個文件顯示的列表,每次文件更新列表都不會顯示到最新的一個。來論壇看了一下,有些朋友已經(jīng)說出了解決的方法,但是沒有一個直觀的程序,這里我po一個自己的程序。關鍵的思路時使用列表的toprow屬性節(jié)點
    發(fā)表于 04-07 16:48

    北京大學首次硬件實現(xiàn)電容耦合的VO2相變振蕩動力學計算系統(tǒng)

    北京大學集成電路學院楊玉超教授課題組首次硬件實現(xiàn)了電容耦合的VO2相變振蕩動力學計算系統(tǒng)。
    的頭像 發(fā)表于 02-28 11:28 ?1052次閱讀
    北京大學首次硬件實現(xiàn)電容耦合的<b class='flag-5'>VO</b>2相變振蕩動力學計算系統(tǒng)

    基于VO2憶阻器的無線物聯(lián)網(wǎng)混合系統(tǒng)

    針對此問題,北京大學集成電路學院/集成電路高精尖創(chuàng)新中心的楊玉超教授團隊首次提出以VO2 憶阻器為主體的高一致性、可校準的頻率振蕩器,在此基礎上構建了8×8的VO2 憶阻器陣列,通過緊湊設計的外圍電路實現(xiàn)了頻率現(xiàn)場生成和可編程頻率混合系統(tǒng)。
    的頭像 發(fā)表于 02-22 09:30 ?786次閱讀
    主站蜘蛛池模板: 看大片全色黄大色黄 | 闲人综合| 777色淫网站女女免费 | 国产毛片农村妇女系列 | 亚洲天天综合 | 上课被同桌摸下面做羞羞 | 男人午夜视频在线观看 | 亚洲免费不卡 | 一区二区三区久久 | 特黄视频| 国产美女精品一区二区三区 | 亚洲国产欧美日韩一区二区三区 | 久久综合爱| 性夜影院午夜看片 | 91在线视频观看 | 亚洲精品美女视频 | 韩日成人 | 中文字幕精品一区二区2021年 | 狠狠色噜噜狠狠狠狠色综合久 | 亚洲色播永久网址大全 | 亚洲我射| 久久99热久久精品 | 亚洲人成综合网站在线 | 三级视频网站在线观看播放 | 亚洲国产成人久久一区久久 | 免费高清成人啪啪网站 | 成人精品人成网站 | 人人搞人人爱 | 天天舔天天摸 | 1000部又爽又黄的做黄禁片 | 国产精品资源网站在线观看 | 一本到卡二卡三卡视频 | 欧美成人免费草草影院 | 日日摸夜夜爽夜夜爽出水 | 男操女视频网站 | 激情综合六月 | 免费的毛片 | 欧美日韩一日韩一线不卡 | 黄色生活毛片 | 天天综合色一区二区三区 | 久久国产午夜精品理论片34页 |