• Java中的dozer对象转换


    Java中的dozer对象转换

    1、dozer介绍

    Dozer是Java Bean到Java Bean映射器,它以递归方式将数据从一个对象复制到另一个对象。 dozer是用来对两个对象之间属性转换的工具,有了这个工具之后,我们将一个对象的所有属性值转给另一个对象时,就不需要再去写重复的调用set和get方法了。dozer其实是对我们熟知的beanutils的封装。

    2、依赖坐标

    <dependency>
        <groupId>com.github.dozermappergroupId>
        <artifactId>dozer-coreartifactId>
        <version>6.5.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    为了简化使用方式,dozer还提供了starter,其maven坐标为:

    <dependency>
        <groupId>com.github.dozermappergroupId>
        <artifactId>dozer-spring-boot-starterartifactId>
        <version>6.5.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3、创建测试工厂【dozer_demo】

    3.1、引入对应的依赖

    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.2.2.RELEASEversion>
            <relativePath/>
        parent>
        <groupId>org.examplegroupId>
        <artifactId>dozer_demoartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>com.github.dozermappergroupId>
                <artifactId>dozer-spring-boot-starterartifactId>
                <version>6.5.0version>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    3.2、创建UserDTO和UserEntity

    dto:用户后端与前端之间的数据交互或后端之间的数据交互
    entity:对应的是数据库列字段属性

    package com.zcl.dto;
    
    import lombok.Data;
    
    /**
     * 项目名称:dozer_demo
     * 描述:用户数据传输对象
     *
     * @author zhong
     * @date 2022-08-29 10:41
     */
    @Data
    public class UserDTO {
        private String userId;
        private String userName;
        private int userAge;
        private String address;
        private String birthday;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    实际项目中的属性命名可能是一致的,而下面的使用属性故意设置为个别属性不一致,测试属性名不一致的时候如何进行对象的转换

    package com.zcl.UserEntity;
    
    import lombok.Data;
    
    import java.util.Date;
    
    /**
     * 项目名称:dozer_demo
     * 描述:用户数据实体
     *
     * @author zhong
     * @date 2022-08-29 10:42
     */
    @Data
    public class UserEntity {
        private String id;
        private String name;
        private int age;
        private String address;
        private Date birthday;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.3、在resources/dozer/目录下创建dozer的全局配置文件global.dozer.xml

    文件名称可以自定义,该配置文件的主要作用是配置全局的一个日期时间格式统一

    
    <mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="http://dozermapper.github.io/schema/bean-mapping"
              xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping 
                                  http://dozermapper.github.io/schema/bean-mapping.xsd">
        
        <configuration>
            <date-format>yyyy-MM-dddate-format>
        configuration>
    mappings>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.4、在resources/dozer/目录下创建dozer的映射文件biz.dozer.xml

    该映射文件的主要作用是:将两个JavaBean不同的属性字段进行描述(谁对应谁)的映射关系
    文件名称可以自定义
    date-format="yyyy-MM-dd"是上一个配置文件所定义的全局格式
    下面的配置文件中通过定义配置文件的全路径名称来指定,如
    com.itheima.entity.UserEntity
    通过field来指定两个对象之间的映射关系

    
    <mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="http://dozermapper.github.io/schema/bean-mapping"
              xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping
                                 http://dozermapper.github.io/schema/bean-mapping.xsd">
        
        <mapping date-format="yyyy-MM-dd">
            <class-a>com.zcl.entity.UserEntityclass-a>
            <class-b>com.zcl.dto.UserDTOclass-b>
            <field>
                <a>ida>
                <b>userIdb>
            field>
            <field>
                <a>namea>
                <b>userNameb>
            field>
            <field>
                <a>agea>
                <b>userAgeb>
            field>
        mapping>
        
        <mapping date-format="yyyy-MM-dd" map-id="user">
            <class-a>com.zcl.entity.UserEntityclass-a>
            <class-b>com.zcl.dto.UserDTOclass-b>
            <field>
                <a>ida>
                <b>userIdb>
            field>
            <field>
                <a>namea>
                <b>userNameb>
            field>
            <field>
                <a>agea>
                <b>userAgeb>
            field>
        mapping>
    mappings>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    3.5、编写application.yml文件

    如果不在配置文件中指定的化,上面编写的两个映射关系配置文件都不会生效

    dozer:
      mappingFiles:
        - classpath:dozer/global.dozer.xml
        - classpath:dozer/biz.dozer.xml
    
    • 1
    • 2
    • 3
    • 4

    3.6、编写项目启动类

    package com.zcl;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * 项目名称:dozer_demo
     * 描述:启动类
     *
     * @author zhong
     * @date 2022-08-29 11:01
     */
    @SpringBootApplication
    public class DozerApp {
        public static void main(String[] args) {
            SpringApplication.run(DozerApp.class, args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3.7、编写单元测试

    package com.zcl.test;
    
    import com.github.dozermapper.core.Mapper;
    import com.zcl.DozerApp;
    import com.zcl.dto.UserDTO;
    import com.zcl.entity.UserEntity;
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    /**
     * 项目名称:dozer_demo
     * 描述:测试类
     *
     * @author zhong
     * @date 2022-08-29 11:03
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = DozerApp.class)
    public class DozerTest {
        /**
         * 注入dozer通过的对象,进行两个对象之间的复制
         */
        @Autowired
        private Mapper mapper;
        @Test
        public void testDozer1(){
            UserDTO userDTO = new UserDTO();
            userDTO.setUserId("100");
            userDTO.setUserName("itcast");
            userDTO.setUserAge(20);
            userDTO.setAddress("bj");
            userDTO.setBirthday("2010-11-20");
    
            // 调用map方法进行复制(原数据,复制的目标对象)
            UserEntity user = mapper.map(userDTO, UserEntity.class);
            System.out.println(user);
            // UserEntity(id=100, name=itcast, age=20, address=bj, birthday=Sat Nov 20 00:00:00 CST 2010)
        }
    
        @Test
        public void testDozer2(){
            UserDTO userDTO = new UserDTO();
            userDTO.setUserId("100");
            userDTO.setUserName("itcast");
            userDTO.setUserAge(20);
            userDTO.setAddress("bj");
            userDTO.setBirthday("2010-11-20");
    
            UserEntity user = new UserEntity();
            user.setId("200");
            System.out.println(user);
            // UserEntity(id=200, name=null, age=0, address=null, birthday=null)
            mapper.map(userDTO,user);
            System.out.println(user);
            // UserEntity(id=100, name=itcast, age=20, address=bj, birthday=Sat Nov 20 00:00:00 CST 2010)
        }
    
        @Test
        public void testDozer3(){
            UserDTO userDTO = new UserDTO();
            userDTO.setUserId("100");
            userDTO.setUserName("itcast");
            userDTO.setUserAge(20);
            userDTO.setAddress("bj");
    
            UserEntity user = new UserEntity();
            System.out.println(user);
            // UserEntity(id=null, name=null, age=0, address=null, birthday=null)
            mapper.map(userDTO,user,"user");
            System.out.println(user);
            // UserEntity(id=100, name=itcast, age=20, address=bj, birthday=null)
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
  • 相关阅读:
    一篇博客搞懂HashMap相关知识
    RAW socket
    仅仅只是用脱虚向实或者脱实向虚来诠释和表达产业互联网是不全面的
    1624. 两个相同字符之间的最长子字符串
    Kafaka核心设计与实践原理(第一部分)
    计算机组成原理学习笔记(1.计算机系统概述 2.数据的表示和运算)
    剑指 Offer II 044. 二叉树每层的最大值
    [云原生k8s] Pod基础概念
    【计算机视觉】使用opencv中dnn模块及openvino2022进行模型推理(C++接口)
    第一周pwn的题目
  • 原文地址:https://blog.csdn.net/baidu_39378193/article/details/126579997