首页 > 文章列表 > SpringBoot如何整合Dozer映射框架

SpringBoot如何整合Dozer映射框架

springboot dozer
197 2023-05-10

SpringBoot如何整合Dozer映射框架

1. Dozer 介绍

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

最重要的是,Dozer 可以确保来自数据库的内部域对象不会渗入外部表示层或外部消费者,它还可以将域对象映射到外部 API 调用,反之亦然。

2. 为什么要使用映射框架 Dozer

映射框架在分层架构中作用很大,我们可以通过封装对特定数据对象的更改与将这些对象传播到其他层(即外部服务数据对象、域对象、数据传输对象、内部服务数据对象)来创建抽象层。 映射框架非常适合在负责将数据从一个数据对象映射到另一个数据对象的 Mapper 类型类中使用。

对于分布式系统架构而言,副作用是域对象在不同系统之间的传递。那么,我们不希望内部域对象暴露在外部,也不允许外部域对象渗入我们的系统。

数据对象之间的映射传统上是通过在对象之间复制数据的手动编码值对象组装器(或转换器)来解决的。Dozer 作为一个强大、通用、灵活、可重用和可配置的开源映射框架,节省了开发人员开发某种自定义映射器框架带来的时间成本。

3. Dozer 映射框架的使用

Dozer 的 maven 坐标:

<dependency>

    <groupId>com.github.dozermapper</groupId>

    <artifactId>dozer-core</artifactId>

    <version>6.5.0</version>

</dependency>

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

<dependency>

    <groupId>com.github.dozermapper</groupId>

    <artifactId>dozer-spring-boot-starter</artifactId>

    <version>6.5.0</version>

</dependency>

下面就开始着手在 springboot 项目中使用 dozer 映射框架。工程的目录结构如下图所示:

第一步,创建 maven 工程 dozer_demo 并配置 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>

<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.0</modelVersion>

    <groupId>com.hzz</groupId>

    <artifactId>dozer_demo</artifactId>

    <version>1.0-SNAPSHOT</version>

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.2.2.RELEASE</version>

        <relativePath/>

    </parent>

    <dependencies>

        <dependency>

            <groupId>com.github.dozermapper</groupId>

            <artifactId>dozer-spring-boot-starter</artifactId>

            <version>6.5.0</version>

        </dependency>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

        </dependency>

        <dependency>

            <groupId>org.projectlombok</groupId>

            <artifactId>lombok</artifactId>

        </dependency>

    </dependencies>

</project>

第二步,创建 UserDTO 和 UserEntity

UserDTO 类

package com.hzz.dto;

import lombok.Data;

@Data

public class UserDTO {

    private String userId;

    private String userName;

    private int userAge;

    private String address;

    private String birthday;

}

UserEntity 类

package com.hzz.entity;

import lombok.Data;

import java.util.Date;

@Data

public class UserEntity {

    private String id;

    private String name;

    private int age;

    private String address;

    private Date birthday;

}

第三步,在 resources/dozer/ 目录下创建 dozer 的全局配置文件 global.dozer.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">

    <!--全局配置:<date-format>表示日期格式-->

    <configuration>

        <date-format>yyyy-MM-dd</date-format>

    </configuration>

</mappings>

第四步,在 resources/dozer/ 目录下创建 dozer 的映射文件 biz.dozer.xml

<?xml version="1.0" encoding="UTF-8"?>

<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.hzz.entity.UserEntity</class-a>

        <class-b>com.hzz.dto.UserDTO</class-b>

        <field>

            <a>id</a>

            <b>userId</b>

        </field>

        <field>

            <a>name</a>

            <b>userName</b>

        </field>

        <field>

            <a>age</a>

            <b>userAge</b>

        </field>

    </mapping>

    <!--

    可以使用 map-id 指定映射的标识,在程序中通过此标识来确定使用当前这个映射关系

    -->

    <mapping date-format="yyyy-MM-dd" map-id="user">

        <class-a>com.hzz.entity.UserEntity</class-a>

        <class-b>com.hzz.dto.UserDTO</class-b>

        <field>

            <a>id</a>

            <b>userId</b>

        </field>

        <field>

            <a>name</a>

            <b>userName</b>

        </field>

        <field>

            <a>age</a>

            <b>userAge</b>

        </field>

    </mapping>

</mappings>

第五步,编写 application.yml 文件

dozer:

  mappingFiles:

    - classpath:dozer/global.dozer.xml

    - classpath:dozer/biz.dozer.xml

第六步,创建主启动类 DozerApp

package com.hzz;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class DozerApp {

    public static void main(String[] args) {

        SpringApplication.run(DozerApp.class, args);

    }

}

第七步,编写单元测试 DozerTest

package com.hzz;

import com.github.dozermapper.core.Mapper;

import com.hzz.dto.UserDTO;

import com.hzz.entity.UserEntity;

import org.junit.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;

@RunWith(SpringRunner.class)

@SpringBootTest(classes = DozerApp.class)

public class DozerTest {

    @Autowired

    private Mapper mapper;

    @Test

    public void testDozer() {

        UserDTO userDTO = new UserDTO();

        userDTO.setUserId("100");

        userDTO.setUserName("ls");

        userDTO.setUserAge(2);

        userDTO.setAddress("bj");

        userDTO.setBirthday("2020-07-04");

        UserEntity user = mapper.map(userDTO, UserEntity.class);

        System.out.println(user);

    }

    @Test

    public void testDozer2(){

        UserDTO userDTO = new UserDTO();

        userDTO.setUserId("100");

        userDTO.setUserName("ls");

        userDTO.setUserAge(5);

        userDTO.setAddress("bj");

        userDTO.setBirthday("2017-07-04");

        UserEntity user = new UserEntity();

        user.setId("200");

        System.out.println(user);

        mapper.map(userDTO, user);

        System.out.println(user); //被 userDTO 覆盖了

    }

    @Test

    public void testDozer3(){

        UserDTO userDTO = new UserDTO();

        userDTO.setUserId("100");

        userDTO.setUserName("zs");

        userDTO.setUserAge(3);

        userDTO.setAddress("bj");

        UserEntity user = new UserEntity();

        System.out.println(user);

        mapper.map(userDTO,user,"user"); //指定映射ID为user

        System.out.println(user);

    }

}