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

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

完善資料讓更多小伙伴認識你,還能領取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的插件不要忘了加上。否則會出現下面的錯誤:No property named "aaa" exists in source parameter(s). Did you mean "null"?

這種異常就是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);
}

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
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 轉換器
    +關注

    關注

    27

    文章

    9007

    瀏覽量

    151310
  • 代碼
    +關注

    關注

    30

    文章

    4894

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

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

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

    CCLINKIE轉PROFINET:電機的“網絡沖浪神器”!

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

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

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

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

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

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

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

    FPGA打磚塊小游戲設計思路

    HDL,?Vivado 平臺上開發打磚塊小游戲并使用 PS2 與 VGA 的基本思路: 一、整體架構設計 1. 輸入模塊: ? PS2 接口模塊:負責與 PS2 設備(如游戲手柄)進行通信,接收手柄
    的頭像 發表于 12-09 16:57 ?902次閱讀

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

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

    監控平臺設計思路

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

    TAS6424-Q1或TAS6424E-Q1 Po_BTL模式PVDD=25V供電狀態下,可以支持2Ω負載?

    咨詢TAS6424-Q1或TAS6424E-Q1Po_BTL模式PVDD=25V供電狀態下,能否支持2Ω負載(有計劃連接座椅振動器,座椅振動器直流阻抗范圍按照:2±20%歐姆;最低值:1.6歐姆
    發表于 10-09 07:34

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

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

    嵌入式澆花神器拆解

    這就是澆花神器,很小巧的一個東西,可以把水桶中的水泵到花盆中。它可以用按鍵手動操作,不過更方便的是通過WiFi連接網絡,這樣就可以通過手機端的APP遠程操作了,還可以定時操作,間隔多長時間澆一次,每次膠水多長時間都可以設置。
    的頭像 發表于 08-29 10:11 ?691次閱讀
    嵌入式澆花<b class='flag-5'>神器</b>拆解

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

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

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

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

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

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

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

    設計參數包括輸入電壓Vin(AC),輸出電壓Vo、輸出功率Po,效率h、開關頻率fs、最大占空比Dmax、工作模式和電流紋波系數K。非連續(斷續)導通模式(DCM),具有更高穩定性和更高效益,最大
    的頭像 發表于 07-02 14:58 ?4281次閱讀
    反激變換器DCM模式變壓器設計及元件選擇
    主站蜘蛛池模板: 日日干狠狠干 | 天天爽夜夜爽免费看 | 国产精品特黄毛片 | 久久精品视频国产 | 日本拍拍视频 | xxxx人成高清免费图片 | 狠狠干夜夜操 | 特黄视频免费看 | 四虎影视免费观看 | 女人张开双腿让男人桶爽免 | 久久久久久免费播放一级毛片 | 四虎4hu永久免费国产精品 | 日本不卡在线视频高清免费 | 九九re| 你懂的在线观看视频 | 又粗又大又爽又色又过瘾视频 | 国产一级毛片午夜 | 韩国三级视频网站 | 四月婷婷七月婷婷综合 | 91色在线视频| 亚洲春色在线 | 亚洲日本精品 | 国产午夜在线观看视频 | 久久综合色综合 | a级精品九九九大片免费看 a级毛毛片看久久 | 男人操女人视频在线观看 | 激情五月开心婷婷 | 国产精品理论 | 夜夜夜操操操 | 成人黄色网址 | 国产精品第一页在线观看 | 精品一区二区三区三区 | 亚洲伊人久久大香线蕉结合 | 97综合视频| 欧美无遮挡一区二区三区 | 日本a级片免费 | 2020年亚洲天天爽天天噜 | 男男生子大肚play做到生 | 天天干天天做天天操 | 日韩成人黄色 | 国产精品网址你懂的 |