Administrator
发布于 2023-01-04 / 47 阅读
0
0

BeanMapping

浅拷贝和深拷贝

1.浅拷贝:对基本数据类型进行值传递,对引用数据类型,使用其引用地址,不拷贝其内容,此为浅拷贝
2.深拷贝:对基本数据类型进行值传递,对引用数据类型,创建一个新的对象,并复制其内容,此为深拷贝。

Spring BeanUtils

方法 说明
BeanUtils.copyProperties(source, target); source对应的对象成员赋值给target对应的对象成员
BeanUtils.copyProperties(source, target, “id”, “time”); 忽略拷贝某些字段。本处是忽略:id与time

111
The following table provides a non-exhaustive set of examples of source and target property types that can be copied as well as source and target property types that cannot be copied.

source property type target property type copy supported
Integer Integer yes
Integer Number yes
List List yes
List<?> List<?> yes
List List<?> yes
List List<? extends Number> yes
String Integer no
Number Integer no
List List no
List List no

属性名 和 属性类型,必须一致

属性对应的属性名和属性类型必须相同,否则对应的属性值不会从一个对象赋值给另一个对象,但是此时不影响其他属性值的拷贝

下面看几个demo

public class SchoolDTO {

    private String name;

    private Integer historyAge;

    private List<StudentDTO> students;


----------------------------

public class SchoolVO {

    private String name;

    private Integer historyAge;

    private List<StudentVO> students;


-----------------------

public static void main(String[] args) throws JsonProcessingException {
        SchoolVO schoolVO = getSchoolVO();

        SchoolDTO schoolDTO = new SchoolDTO();
        BeanUtils.copyProperties(schoolVO,schoolDTO);

        ObjectMapper objectMapper = new ObjectMapper();

// 输出: {"name":"实验小学","historyAge":66,"students":null}
        System.out.println(objectMapper.writeValueAsString(schoolDTO));

    }

上面的demo中,students这个属性,类型不同,这个属性就不再拷贝了

BeanUtils-Spring 属于浅拷贝

public static void main(String[] args) throws JsonProcessingException {
        SchoolVO schoolVO = getSchoolVO();

        ObjectMapper objectMapper = new ObjectMapper();
        SchoolDTO schoolDTO = new SchoolDTO();
        BeanUtils.copyProperties(schoolVO,schoolDTO);


        System.out.println(objectMapper.writeValueAsString(schoolDTO));
        // 两次输出不同,代表了,这个其实是浅拷贝,即对于 引用类型 数据,使用其引用地址,不拷贝其内容
        List<StudentVO> students = schoolVO.getStudents();
        students.get(0).setName("lisi");

        System.out.println(objectMapper.writeValueAsString(schoolDTO));

    }

输出


{"name":"实验小学","historyAge":66,"students":[{"name":"zhangsan","age":25,"score":82.5}]}
{"name":"实验小学","historyAge":66,"students":[{"name":"lisi","age":25,"score":82.5}]}

多余的属性,会被自动忽略


The source and target classes do not have to match or even be derived from each other, as long as the properties match. 

Any bean properties that the source bean exposes but the target bean does not will silently be ignored.

主动设置,忽略某些属性

public static void main(String[] args) throws JsonProcessingException {
        SchoolVO schoolVO = getSchoolVO();

        ObjectMapper objectMapper = new ObjectMapper();
        SchoolDTO schoolDTO = new SchoolDTO();

        // 忽略某些属性
        String[] ignoreProperties = new String[]{"name","students"};

        BeanUtils.copyProperties(schoolVO,schoolDTO,ignoreProperties);

        System.out.println(objectMapper.writeValueAsString(schoolDTO));

    }

输出

{"name":null,"historyAge":66,"students":null}

BeanWrapper



Vo2dto

refer to : https://gitee.com/fustack/vo2dto

实测,非常好用。推荐使用



VO & DTO & DO & PO区分

refer to :

https://iditect.com/article/conversion-between-vo-and-dto-in-java-various-tool-classes.html

https://www.w3docs.com/snippets/java/difference-between-dto-vo-pojo-javabeans.html

https://www.nowcoder.com/discuss/353150162468020224


  1. VO (View Object), view object, used for the display layer, its function is to encapsulate all the data of a specified page (or component).

  2. DTO (Data Transfer Object), data transfer object,I generally refer to the data transfer object between the presentation layer and the service layer.

  3. BO (Business Object), 业务对象,把业务逻辑封装为一个对象,这个对象可以包括一个或多个其它的对象。

  4. PO (PersistentObject), a persistent object, which forms a one-to-one mapping relationship with the data structure of the persistence layer (usually a relational database).
    If the persistence layer is a relational database, then each field in the data table (or several) correspond to one (or several) attributes of PO.



注意:
BO中, many fields such as type are enumerations.

DTO is like a webservice interface (providing external services), and the returned result objects are all DTOs.

VO is relatively simple, you can define whatever is needed for the front-end display



结合,我们实际项目中,

前端的请求体,controller中使用dto接收参数,处理好后,响应体使用vo,提供给前端

而service层的方法,输入的结果是dto,输出的结果使用BO

dao中的方法,输入的结果是PO,输出的结果是PO

  • 如果是前端,请求controller,那么controller使用ReqDto接收参数,RespDto返回结果

  • 如果涉及到了微服务之间的调用,服务A调用服务B,那么服务B使用Dto接收参数,使用Dto返回结果。


评论