• Spring--BeanUtils工具类--使用/实例


    原文网址:Spring--BeanUtils工具类--使用/实例_IT利刃出鞘的博客-CSDN博客

    简介

    说明

            本文介绍Spring的BeanUtils工具类的用法。

            我们经常需要将不同的两个对象实例进行属性复制,比如将DO对象进行属性复制到DTO,这种转换最原始的方式就是手动编写大量的 get/set代码,很繁琐。为了解决这一痛点,就诞生了一些方便的类库,常用的有 Apache的 BeanUtils,Spring的 BeanUtils, Dozer,Orika等拷贝工具。

            由于Apache的BeanUtils的性能很差,强烈不建议使用。阿里巴巴Java开发规约插件上也明确指出:
    “Ali-Check | 避免用Apache Beanutils进行属性的copy。”

    Spring的BeanUtils方法

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

    Spring的BeanUtils方法注意事项 

    泛型只在编译期起作用,不能依靠泛型来做运行期的限制; 

    浅拷贝和深拷贝

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

    Spring的BeanUtils与Apache的BeanUtils区别

    Spring的BeanUtilsApache的BeanUtils
    性能

    好。

    原因:对两个对象中相同名字的属性进行简单的get/set,仅检查属性的可访问性

    差。

    原因:有很多检验:类型的转换、对象所属类的可访问性等

    注意:本处类型转换是类似的类,多个String转为List<String>是不行的。

    使用方面

    第一个参数是源,第二个参数是目标。

    无需捕获异常

    第一个参数是目标,第二个参数是源。

    必须捕获异常

    深/浅拷贝浅拷贝浅拷贝

    Apache的BeanUtils方法。(依赖:maven里直接搜“commons-beanutils”)

    方法说明
    BeanUtils.copyProperties(Object target, Object source);source对应的对象成员赋值给target对应的对象成员
    BeanUtils.copyProperties(Object target, String name, Object source);只拷贝某些字段。

    Apache的BeanUtils支持的类型转换

    1. java.lang.BigDecimal
    2. java.lang.BigInteger
    3. boolean and java.lang.Boolean
    4. byte and java.lang.Byte
    5. char and java.lang.Character
    6. java.lang.Class
    7. double and java.lang.Double
    8. float and java.lang.Float
    9. int and java.lang.Integer
    10. long and java.lang.Long
    11. short and java.lang.Short
    12. java.lang.String
    13. java.sql.Date
    14. java.sql.Time
    15. java.sql.Timestamp

            java.util.Date是不被支持的,而它的子类java.sql.Date是被支持的。因此如果对象包含时间类型的属性,且希望被转换的时候,一定要使用java.sql.Date类型。否则在转换时会提示argument mistype异常。

    实例

    entity

    1. package com.example.entity;
    2. import lombok.Data;
    3. @Data
    4. public class Question {
    5. private Integer id;
    6. private Integer studentId;
    7. private String content;
    8. private Integer value;
    9. }
    1. package com.example.entity;
    2. import lombok.Data;
    3. @Data
    4. public class Student {
    5. private Integer id;
    6. private String name;
    7. private Integer points;
    8. }

    vo

    1. package com.example.vo;
    2. import lombok.Data;
    3. import java.io.Serializable;
    4. import java.util.List;
    5. @Data
    6. public class QuestionStudentVO implements Serializable {
    7. private Integer id;
    8. private String content;
    9. private Integer value;
    10. private Integer studentId;
    11. private List<String> name;
    12. private Integer points;
    13. }

     测试类

    1. package com.example;
    2. import com.example.entity.Question;
    3. import com.example.entity.Student;
    4. import com.example.vo.QuestionStudentVO;
    5. import org.junit.Test;
    6. import org.junit.runner.RunWith;
    7. import org.springframework.beans.BeanUtils;
    8. import org.springframework.boot.test.context.SpringBootTest;
    9. import org.springframework.test.context.junit4.SpringRunner;
    10. @RunWith(SpringRunner.class)
    11. @SpringBootTest
    12. public class DemoApplicationTests {
    13. @Test
    14. public void beanUtilTest(){
    15. Question question = new Question();
    16. question.setId(2);
    17. // question.setStudentId(3);
    18. question.setContent("This is content");
    19. question.setValue(100);
    20. Student student = new Student();
    21. student.setId(3);
    22. student.setName("Tony Stark");
    23. student.setPoints(201);
    24. QuestionStudentVO questionStudentVO = new QuestionStudentVO();
    25. BeanUtils.copyProperties(question, questionStudentVO);
    26. BeanUtils.copyProperties(student, questionStudentVO);
    27. System.out.println(questionStudentVO);
    28. }
    29. }

    执行结果

    QuestionStudentVO(id=3, content=This is content, value=100, studentId=null, name=null, points=201)

  • 相关阅读:
    代码随想录笔记_链表_24两两交换链表中的节点
    视频融合平台EasyCVR接入大华SDK时无法接入设备通道该如何解决?
    书店管理系统
    【树莓派不吃灰】SSH 连接报错“WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!”的解决
    python 微信小程序 英语单词小程序代码分享
    生产环境nsx-v升级为nsx-t的一些问题
    DP之字符串算法
    【干货】浅谈如何给.net程序加多层壳达到1+1>2的效果
    数据结构与算法——串
    【微信小程序】初始微信小程序
  • 原文地址:https://blog.csdn.net/feiying0canglang/article/details/125463849