SpringBoot中DTO/VO/Entity相互轉(zhuǎn)換詳解
在我們平時(shí)開發(fā)中,dto、vo、entity之間的相互轉(zhuǎn)換是很頻繁的操作,這篇就簡(jiǎn)單記錄一下我在平時(shí)開發(fā)中轉(zhuǎn)換的方法。
在這之前先簡(jiǎn)單描述一下dto、vo、entity
DTO:一般我們?cè)陂_發(fā)中會(huì)定義數(shù)據(jù)傳輸對(duì)象(Data Transfer Object, DTO)來接收前端傳遞的參數(shù)是最常見的。
VO:平時(shí)開發(fā)中,一般會(huì)定義VO(view object)來封裝返回給前端的數(shù)據(jù)
Entity:在我們開發(fā)中,Entity適用于表示持久化對(duì)象的核心組件之一。它通常與數(shù)據(jù)庫中的表直接對(duì)應(yīng),用于映射數(shù)據(jù)庫記錄到應(yīng)用程序的對(duì)象
我們?cè)谄綍r(shí)的增刪改查中,簡(jiǎn)單來說就是
就像新增操作,一般會(huì)使用dto來接受前端傳遞的參數(shù),然后將dto轉(zhuǎn)為Entity,再set一些創(chuàng)建時(shí)間之類的字段,然后再將Entity實(shí)體對(duì)象數(shù)據(jù)插入到數(shù)據(jù)庫中。
查詢操作,先從數(shù)據(jù)庫查詢出數(shù)據(jù)即Entity,然后再將Entity轉(zhuǎn)為VO視圖數(shù)據(jù),再返回給前端。
我這里平時(shí)開發(fā)中,會(huì)使用下面封裝的工具類來進(jìn)行dto、vo、entity之間的轉(zhuǎn)換,代碼如下:
首先pom.xml引入依賴:
<!-- JSON 解析器和生成器 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency>
封裝JSONUtil類
package com.wft.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.parser.Feature; import com.alibaba.fastjson.serializer.SerializerFeature; import java.util.List; import java.util.Map; public class JsonUtil { public JsonUtil() { } public static List listToJsonField(List lists) { String jsonStr = JSONArray.toJSONString(lists, new SerializerFeature[]{SerializerFeature.WriteMapNullValue}); List list = (List)JSONArray.parseObject(jsonStr, List.class); return list; } public static Map<String, Object> entityToMap(Object object) { String jsonStr = JSONObject.toJSONString(object); Map<String, Object> map = (Map)JSONObject.parseObject(jsonStr, new TypeReference<Map<String, Object>>() { }, new Feature[0]); return map; } public static Map<String, String> entityToMaps(Object object) { String jsonStr = JSONObject.toJSONString(object); Map<String, String> map = (Map)JSONObject.parseObject(jsonStr, new TypeReference<Map<String, String>>() { }, new Feature[0]); return map; } public static Map<String, Object> stringToMap(String object) { Map<String, Object> map = (Map)JSONObject.parseObject(object, new TypeReference<Map<String, Object>>() { }, new Feature[0]); return map; } public static <T> T getJsonToBean(String jsonData, Class<T> clazz) { return JSON.parseObject(jsonData, clazz); } public static JSONArray getJsonToJsonArray(String json) { return JSONArray.parseArray(json); } public static <T> JSONArray getListToJsonArray(List<T> list) { return JSONArray.parseArray(getObjectToString(list)); } public static String getObjectToString(Object object) { return JSON.toJSONString(object, new SerializerFeature[]{SerializerFeature.WriteMapNullValue}); } public static String getObjectToStringDateFormat(Object object, String dateFormat) { return JSON.toJSONStringWithDateFormat(object, dateFormat, new SerializerFeature[]{SerializerFeature.WriteMapNullValue}); } public static <T> List<T> getJsonToList(String jsonData, Class<T> clazz) { return JSON.parseArray(jsonData, clazz); } public static List<Map<String, Object>> getJsonToListMap(String jsonData) { return (List)JSON.parseObject(jsonData, new TypeReference<List<Map<String, Object>>>() { }, new Feature[0]); } public static List<Map<String, Object>> getJsonToList(JSONArray jsonArray) { return (List)JSON.parseObject(JSON.toJSONString(jsonArray), new TypeReference<List<Map<String, Object>>>() { }, new Feature[0]); } public static <T> T getJsonToBean(Object dto, Class<T> clazz) { return JSON.parseObject(getObjectToString(dto), clazz); } public static <T> List<T> getJsonToList(Object dto, Class<T> clazz) { return JSON.parseArray(getObjectToString(dto), clazz); } }
平時(shí)使用中我一般使用最多的就是JsonUtil.getJsonToBean方法,就是進(jìn)行dto、vo、entity對(duì)象之間的相互轉(zhuǎn)換,簡(jiǎn)單聚個(gè)例子如下:
示例:
前端傳遞DTO(name、age、birthTime)過來,然后我們轉(zhuǎn)為VO(ame、age、birthTime、creatorTime)并返回。
DTO類:
package com.wft.model.testJson; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; @Data public class TestJsonForm { /** 姓名 **/ @JsonProperty("name") private String name; /** 年齡 **/ @JsonProperty("age") private Integer age; /** 出生日期 **/ @JsonProperty("birthTime") private String birthTime; }
VO:
package com.wft.model.testJson; import com.alibaba.fastjson.annotation.JSONField; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.util.Date; @Data public class TestJsonVo { @JSONField(name = "name") private String name; @JSONField(name = "age") private Integer age; @JSONField(name = "birthTime") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date birthTime; @JSONField(name = "birthTime") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") private Date creatorTime; }
Controller:
package com.wft.controller; import com.wft.model.ActionResult; import com.wft.model.testJson.TestJsonForm; import com.wft.model.testJson.TestJsonVo; import com.wft.utils.JsonUtil; import org.springframework.web.bind.annotation.*; import java.util.Date; @RestController @RequestMapping("/json") public class TestJsonController { @PostMapping("/save") public ActionResult uploadTest(@RequestBody TestJsonForm testJsonForm) { // 將dto轉(zhuǎn)為vo TestJsonVo vo = JsonUtil.getJsonToBean(testJsonForm, TestJsonVo.class); vo.setCreatorTime(new Date()); return ActionResult.success(vo); } }
@JsonProperty
大家能看到DTO中,我使用了 @JsonProperty 注解,這個(gè)很簡(jiǎn)單,其實(shí)就是標(biāo)識(shí)前端傳遞的參數(shù)字段是什么,就是允許我們后端接受的參數(shù)字段和前端傳遞的字段可以不一致,如果不一致我們后端就可以使用@JsonProperty進(jìn)行映射。
上面我們定義的name字段,如果前端傳遞的字段為cusname,我們就可以在name字段上使用這樣的注解@JsonProperty("cusname")這樣設(shè)置。
@JSONField
同樣的我在VO中使用@JSONField注解,上面的例子是把DTO轉(zhuǎn)為VO,VO中的@JSONField對(duì)應(yīng)的就是DTO中的字段
其實(shí)我們平時(shí)開發(fā)中,只要字段都對(duì)應(yīng)好,完全不需要使用這兩個(gè)注解。
上面VO中還使用了@JsonFormat 注解,這個(gè)是格式化時(shí)間的,將Date類型轉(zhuǎn)為String類型yyyy-MM-dd HH:mm:ss返回給前端。
以上就是SpringBoot中DTO/VO/Entity相互轉(zhuǎn)換詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot DTO VO Entity相互轉(zhuǎn)換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Springboot-admin整合Quartz實(shí)現(xiàn)動(dòng)態(tài)管理定時(shí)任務(wù)的過程詳解
Quartz是一款Java編寫的開源任務(wù)調(diào)度框架,同時(shí)它也是Spring默認(rèn)的任務(wù)調(diào)度框架,它的作用其實(shí)類似于Timer定時(shí)器以及ScheduledExecutorService調(diào)度線程池,這篇文章主要介紹了Springboot-admin整合Quartz實(shí)現(xiàn)動(dòng)態(tài)管理定時(shí)任務(wù),需要的朋友可以參考下2023-04-04Springcould多模塊搭建Eureka服務(wù)器端口過程詳解
這篇文章主要介紹了Springcould多模塊搭建Eureka服務(wù)器端口過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11SpringSecurity實(shí)現(xiàn)動(dòng)態(tài)權(quán)限校驗(yàn)的過程
Spring Security過濾器鏈中,AuthorizationFilter的authorizationManager是我們要找的組件,該組件的check方法已被棄用,推薦使用authorize方法,最終通過接口路徑和權(quán)限進(jìn)行校驗(yàn),本文給大家介紹SpringSecurity實(shí)現(xiàn)動(dòng)態(tài)權(quán)限校驗(yàn)的相關(guān)知識(shí),感興趣的朋友一起看看吧2025-02-02詳解Kotlin和anko融合進(jìn)行Android開發(fā)
本篇文章主要介紹了Kotlin和anko融合進(jìn)行Android開發(fā),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11詳解RSA加密算法的原理與Java實(shí)現(xiàn)
這篇文章主要和大家分享非對(duì)稱加密中的一種算法,那就是 RSA 加密算法。本文介紹了RSA算法的原理與Java實(shí)現(xiàn),感興趣的小伙伴可以嘗試一下2022-10-10SpringSecurity6.4中一次性令牌登錄(One-Time Token Login)實(shí)現(xiàn)
Spring Security為一次性令牌認(rèn)證提供了支持,本文就來介紹一下SpringSecurity6.4中一次性令牌登錄(One-Time Token Login)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03Java任意長(zhǎng)度byte數(shù)組轉(zhuǎn)換為int數(shù)組的方法
這篇文章主要給大家介紹了關(guān)于Java任意長(zhǎng)度byte數(shù)組轉(zhuǎn)換為int數(shù)組的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07