Java實(shí)體類之間的相互轉(zhuǎn)換方式
Java實(shí)體類之間的相互轉(zhuǎn)換
為什么要實(shí)體類之間相互轉(zhuǎn)換呢?
1.防止數(shù)據(jù)的泄露,比如使用Hibernate查詢用戶表,用戶表有用戶的賬號(hào)和密碼,我們后端給前端數(shù)據(jù)的時(shí)候并不希望給前端傳遞密碼這個(gè)字段。
2.防止數(shù)據(jù)過大,前端無法接收,hibernate使用的是關(guān)聯(lián)查詢,如果關(guān)聯(lián)的表非常多的話前端接收會(huì)報(bào)錯(cuò)。
3.更好的前后端分離,前端不用考慮后端的業(yè)務(wù)邏輯。而后端只需要給出返回的結(jié)果是否成功的編碼和具體提示內(nèi)容以及其它信息。
在pom.xml配置文件中添加依賴
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.4-jre</version> </dependency>
創(chuàng)建第一個(gè)實(shí)體類School
public class School { private Integer schoolId; private String schoolName; private String name; //省略getter和setter方法 }
創(chuàng)建第二個(gè)實(shí)體類
public class SchoolDTO { private Integer schoolId; private String schoolName; //省略getter和setter方法 //創(chuàng)建轉(zhuǎn)換方法 public School convertTo() { OrderConverter convert = new OrderConverter(); return convert.convert(this); } public SchoolDTO convertFrom(School order) { OrderConverter convert = new OrderConverter(); return convert.reverse().convert(order); } private static class OrderConverter extends Converter<SchoolDTO, School> { @Override protected School doForward(SchoolDTO schoolDTO) { return null; } //具體的轉(zhuǎn)換內(nèi)容 @Override protected SchoolDTO doBackward(School school) { SchoolDTO dto = new SchoolDTO(); dto.setSchoolId(school.getSchoolId()); dto.setSchoolName(school.getSchoolName()); return dto; } } }
創(chuàng)建測(cè)試類(一條數(shù)據(jù)轉(zhuǎn)換)
public class test { public static void main(String[] args) { //一條數(shù)據(jù)轉(zhuǎn)換 School school = new School(); school.setSchoolId(1); school.setSchoolName("上海理工大學(xué)"); school.setName("李四"); SchoolDTO dto = new SchoolDTO().convertFrom(school); System.out.println(dto); //Page類型數(shù)據(jù)轉(zhuǎn)換 //schools.map(s -> new SchoolDTO().convertFrom(s)); //List類型 /* List<SchoolDTO> list = dto.stream().map(address -> { return new SchoolDTO().convertFrom(address); }).collect(Collectors.toList()); */ }
輸入結(jié)果:
SchoolDTO{schoolId=1, schoolName='上海理工大學(xué)'}
第二種:直接使用this
1、首先創(chuàng)建一個(gè)操作數(shù)據(jù)庫(kù)的實(shí)體類:School,和用來給前端傳遞內(nèi)容的實(shí)體類SchoolDTO
public class School { private Integer id; private String schoolName; private String address; //省略getter和setter方法 } public class SchoolDTO { private Integer id; private String schoolName; private String address; //省略getter和setter方法 //省略toString方法 //轉(zhuǎn)換方法 public SchoolDTO toSchool(School dto){ this.setId(dto.getId()); this.setAddress("轉(zhuǎn)換的結(jié)果是:"+dto.getAddress()); this.setSchoolName("轉(zhuǎn)換的結(jié)果是:"+dto.getSchoolName()); return this; } }
2、測(cè)試方法進(jìn)行測(cè)試
public class TestController { public static void main(String[] args) { School school = new School(); school.setId(1); school.setSchoolName("北京大學(xué)"); school.setAddress("北京"); //打印內(nèi)容 System.out.println(new SchoolDTO().toSchool(school)); } }
3、打印結(jié)果
這種方法比較方便,不用引用jar的依賴
實(shí)體類相互轉(zhuǎn)換工具類
場(chǎng)景
不同的實(shí)體類之間取相同數(shù)據(jù)賦值
package com.lrhealth.mappingintegration.utils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @Slf4j public class BeanHelper { public static <T> T copyProperties(Object source, Class<T> target){ try { T t = target.newInstance(); BeanUtils.copyProperties(source, t); return t; } catch (Exception e) { log.error("【數(shù)據(jù)轉(zhuǎn)換】數(shù)據(jù)轉(zhuǎn)換出錯(cuò),目標(biāo)對(duì)象{}構(gòu)造函數(shù)異常", target.getName(), e); return null; } } public static <T> List<T> copyWithCollection(List<?> sourceList, Class<T> target){ try { return sourceList.stream().map(s -> copyProperties(s, target)).collect(Collectors.toList()); } catch (Exception e) { log.error("【數(shù)據(jù)轉(zhuǎn)換】數(shù)據(jù)轉(zhuǎn)換出錯(cuò),目標(biāo)對(duì)象{}構(gòu)造函數(shù)異常", target.getName(), e); return null; } } public static <T> Set<T> copyWithCollection(Set<?> sourceList, Class<T> target){ try { return sourceList.stream().map(s -> copyProperties(s, target)).collect(Collectors.toSet()); } catch (Exception e) { log.error("【數(shù)據(jù)轉(zhuǎn)換】數(shù)據(jù)轉(zhuǎn)換出錯(cuò),目標(biāo)對(duì)象{}構(gòu)造函數(shù)異常", target.getName(), e); return null; } } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用freemarker導(dǎo)出word文件方法詳解
這篇文章主要介紹了SpringBoot使用freemarker導(dǎo)出word文件方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-11-11Spring Boot實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了Spring Boot實(shí)現(xiàn)圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05從零構(gòu)建可視化jar包部署平臺(tái)JarManage教程
這篇文章主要為大家介紹了從零構(gòu)建可視化jar包部署平臺(tái)JarManage教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05java微信開發(fā)API第二步 獲取和回復(fù)消息
這篇文章主要為大家詳細(xì)介紹了java微信開發(fā)API第二步,獲取消息和回復(fù)消息,感興趣的小伙伴們可以參考一下2016-06-06Intellij Idea中批量導(dǎo)入第三方j(luò)ar包的全過程
引入jar包一般都是針對(duì)小的java項(xiàng)目,這篇文章主要給大家介紹了關(guān)于Intellij Idea中批量導(dǎo)入第三方j(luò)ar包的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10SpringBoot使用Async注解失效原因分析及解決(spring異步回調(diào))
這篇文章主要介紹了SpringBoot使用Async注解失效原因分析及解決(spring異步回調(diào)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java 獲取指定日期的實(shí)現(xiàn)方法總結(jié)
以下是對(duì)Java中獲取指定日期的實(shí)現(xiàn)方法進(jìn)行了歸納總結(jié),需要的朋友可以參考下2013-07-07