springboot中DTO、VO、Entity相互转换

在我们平时开发中,dto、vo、entity之间的相互转换是很频繁的操作,这篇就简单记录一下我在平时开发中转换的方法。

在这之前先简单描述一下dto、vo、entity

DTO:一般我们在开发中会定义数据传输对象(Data Transfer Object, DTO)来接收前端传递的参数是最常见的。

VO:平时开发中,一般会定义VO(view object)来封装返回给前端的数据

Entity:在我们开发中,Entity适用于表示持久化对象的核心组件之一。它通常与数据库中的表直接对应,用于映射数据库记录到应用程序的对象

我们在平时的增删改查中,简单来说就是

就像新增操作,一般会使用dto来接受前端传递的参数,然后将dto转为Entity,再set一些创建时间之类的字段,然后再将Entity实体对象数据插入到数据库中。

查询操作,先从数据库查询出数据即Entity,然后再将Entity转为VO视图数据,再返回给前端。

我这里平时开发中,会使用下面封装的工具类来进行dto、vo、entity之间的转换,代码如下:

首先pom.xml引入依赖:



    com.alibaba
    fastjson
    1.2.83

封装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 entityToMap(Object object) {
        String jsonStr = JSONObject.toJSONString(object);
        Map map = (Map)JSONObject.parseObject(jsonStr, new TypeReference>() {
        }, new Feature[0]);
        return map;
    }

    public static Map entityToMaps(Object object) {
        String jsonStr = JSONObject.toJSONString(object);
        Map map = (Map)JSONObject.parseObject(jsonStr, new TypeReference>() {
        }, new Feature[0]);
        return map;
    }

    public static Map stringToMap(String object) {
        Map map = (Map)JSONObject.parseObject(object, new TypeReference>() {
        }, new Feature[0]);
        return map;
    }

    public static  T getJsonToBean(String jsonData, Class clazz) {
        return JSON.parseObject(jsonData, clazz);
    }

    public static JSONArray getJsonToJsonArray(String json) {
        return JSONArray.parseArray(json);
    }

    public static  JSONArray getListToJsonArray(List 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  List getJsonToList(String jsonData, Class clazz) {
        return JSON.parseArray(jsonData, clazz);
    }

    public static List> getJsonToListMap(String jsonData) {
        return (List)JSON.parseObject(jsonData, new TypeReference>>() {
        }, new Feature[0]);
    }

    public static List> getJsonToList(JSONArray jsonArray) {
        return (List)JSON.parseObject(JSON.toJSONString(jsonArray), new TypeReference>>() {
        }, new Feature[0]);
    }

    public static  T getJsonToBean(Object dto, Class clazz) {
        return JSON.parseObject(getObjectToString(dto), clazz);
    }

    public static  List getJsonToList(Object dto, Class clazz) {
        return JSON.parseArray(getObjectToString(dto), clazz);
    }
}

平时使用中我一般使用最多的就是JsonUtil.getJsonToBean方法,就是进行dto、vo、entity对象之间的相互转换,简单聚个例子如下:

示例:

前端传递DTO(name、age、birthTime)过来,然后我们转为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转为vo
        TestJsonVo vo = JsonUtil.getJsonToBean(testJsonForm, TestJsonVo.class);
        vo.setCreatorTime(new Date());
        return ActionResult.success(vo);
    }

}

@JsonProperty

大家能看到DTO中,我使用了 @JsonProperty 注解,这个很简单,其实就是标识前端传递的参数字段是什么,就是允许我们后端接受的参数字段和前端传递的字段可以不一致,如果不一致我们后端就可以使用@JsonProperty进行映射。

上面我们定义的name字段,如果前端传递的字段为cusname,我们就可以在name字段上使用这样的注解@JsonProperty("cusname")这样设置。

@JSONField

同样的我在VO中使用@JSONField注解,上面的例子是把DTO转为VO,VO中的@JSONField对应的就是DTO中的字段

其实我们平时开发中,只要字段都对应好,完全不需要使用这两个注解。 

上面VO中还使用了@JsonFormat 注解,这个是格式化时间的,将Date类型转为String类型yyyy-MM-dd HH:mm:ss返回给前端。 

好,这篇就这样儿~~~

你可能感兴趣的:(后端,springboot,java,spring,boot,后端,java)