SpringBoot2.x 集成 Dozer

Posted RtxTitanV

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot2.x 集成 Dozer相关的知识,希望对你有一定的参考价值。

Dozer是Java Bean到Java Bean的映射器,它以递归的方式将数据从一个对象复制到另一个对象。通常,这些Java Bean将具有不同的复杂类型。它支持简单属性映射,复杂类型映射,双向映射,隐式显式映射,以及递归映射。这包括映射需要在元素层面上进行映射的集合属性。可以将Dozer用作两个对象之间属性转换的工具,使用它可以很方便地对项目中的DO、DTO、VO进行相互转换。

本文主要对SpringBoot2.x集成Dozer及其基本使用进行简单总结,其中SpringBoot使用的2.4.5版本。

一、引入依赖

<dependency>
    <groupId>com.github.dozermapper</groupId>
    <artifactId>dozer-spring-boot-starter</artifactId>
    <version>6.5.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<!-- lombok插件 -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.8</version>
</dependency>

二、实体类

User类:

package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;

/**
 * @author rtxtitanv
 * @version 1.0.0
 * @name com.rtxtitanv.model.User
 * @description User
 * @date 2021/8/18 16:45
 */
@Accessors(chain = true)
@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String gender;
    private String email;
    private Date birthday;
}

UserDTO类:

package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author rtxtitanv
 * @version 1.0.0
 * @name com.rtxtitanv.model.UserDTO
 * @description UserDTO
 * @date 2021/8/18 16:45
 */
@Accessors(chain = true)
@Data
public class UserDTO {
    private Long userId;
    private String userName;
    private Integer userAge;
    private String gender;
    private String email;
    private String 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 HH:mm:ss</date-format>
    </configuration>
</mappings>

resources/dozer/目录下创建Dozer的映射文件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 HH:mm:ss">
        <class-a>com.rtxtitanv.model.User</class-a>
        <class-b>com.rtxtitanv.model.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>

resources目录下创建application.yml配置文件:

dozer:
  # 指定Dozer的映射配置文件位置
  mapping-files:
    - classpath:dozer/global-dozer.xml
    - classpath:dozer/dozer.xml

四、创建测试类

创建单元测试类DozerTest

package com.rtxtitanv;

import com.github.dozermapper.core.Mapper;
import com.rtxtitanv.model.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * @author rtxtitanv
 * @version 1.0.0
 * @name com.rtxtitanv.DozerTest
 * @description Dozer单元测试类
 * @date 2021/8/18 16:44
 */
@Slf4j
@SpringBootTest
class DozerTest {

    @Resource
    private Mapper mapper;

    @Test
    void test1() {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(1L).setUserName("ZhaoYun").setGender("男").setUserAge(20).setEmail("zhaoyun@xxx.com")
            .setBirthday("2001/8/18 18:05:32");
        User user = mapper.map(userDTO, User.class);
        log.info(user.toString());
        UserDTO userDTO2 = mapper.map(user, UserDTO.class);
        log.info(userDTO2.toString());
    }
}

执行测试方法,发现User和UserDTO相互转换成功:

五、Dozer的基本使用

下面对Dozer的一些基本使用进行总结。Dozer支持注解、API、XML三种映射配置方式,XML方式比较常用,前面使用的也是XML映射配置方式。XML映射配置中mapping元素的map-id属性可以设置该映射的标识,通过此标识来确定使用该映射关系。

dozer.xml中新增以下配置:

<!-- map-id:映射的标识,通过此标识来确定使用该映射关系 -->
<mapping date-format="yyyy/MM/dd HH:mm:ss" map-id="user">
    <class-a>com.rtxtitanv.model.User</class-a>
    <class-b>com.rtxtitanv.model.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>

新增以下测试方法:

@Test
void test2() {
    UserDTO userDTO = new UserDTO();
    userDTO.setUserId(2L).setUserName("MaChao").setGender("男").setUserAge(21).setEmail("machao@xxx.com")
        .setBirthday("2000/6/15 08:45:20");
    User user = mapper.map(userDTO, User.class, "user");
    log.info(user.toString());
}

执行测试方法,发现转换成功:

在调用map方法时也可以直接指定要转换的目标对象。新增以下测试方法:

@Test
void test3() {
    UserDTO userDTO = new UserDTO();
    userDTO.setUserId(3L).setUserName("LiuBei").setGender("男").setUserAge(30).setEmail("liubei@xxx.com")
        .setBirthday("1991/1/20 13:36:55");
    User user = new User();
    mapper.map(userDTO, user, "user");
    log.info(user.toString());
}

执行测试方法,发现转换成功:

通过field-exclude标签可以设置不想进行转换的属性,这些属性在进行转换时会被自动排除。

dozer.xml中新增以下配置:

<mapping date-format="yyyy/MM/dd HH:mm:ss" map-id="user-exclude">
    <class-a>com.rtxtitanv.model.User</class-a>
    <class-b>com.rtxtitanv.model.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>
    <field-exclude>
        <a>email</a>
        <b>email</b>
    </field-exclude>
</mapping>

新增以下测试方法:

@Test
void test4() {
    UserDTO userDTO = new UserDTO();
    userDTO.setUserId(1L).setUserName("ZhaoYun").setGender("男").setUserAge(20).setEmail("zhaoyun@xxx.com")
        .setBirthday("2001/8/18 18:05:32");
    User user = mapper.map(userDTO, User.class, "user-exclude");
    log.info(user.toString());
}

执行测试方法,发现email属性被成功排除:

Dozer中的映射方式默认都是双向映射,如果想让转换不可逆,即只需要单向转换,可以设置mapping元素的type属性为one-way来开启单向映射。

dozer.xml中新增以下配置:

<!-- type="one-way"将映射设置为单向映射 -->
<mapping date-format="yyyy/MM/dd HH:mm:ss" map-id="user-oneway" type="one-way">
    <class-a>com.rtxtitanv.model.UserDTO</class-a>
    <class-b>com.rtxtitanv.model.User</class-b>
    <field>
        <a>userId</a>
        <b>id</b>
    </field>
    <field>
        <a>userName</a>
        <b>name</b>
    </field>
    <field>
        <a>userAge</a>
        <b>age</b>
    </field>
</mapping>

新增以下测试方法:

@Test
void test5() {
    UserDTO userDTO = new UserDTO();
    userDTO.setUserId(1L).setUserName("ZhaoYun").setGender("男").setUserAge(20).setEmail("zhaoyun@xxx.com")
        .setBirthday("2001/8/18 18:05:32");
    User user = mapper.map(userDTO, User.class, "user-oneway");
    log.info(user.toString());
    UserDTO userDTO2 = mapper.map(user, UserDTO.class, "user-oneway");
    log.info(userDTO2.toString());
}

执行测试方法,发现只有UserDTO转换为User成功:

当两个实体类中都嵌套有能够互相转换的实体类型属性时,也可以进行相互转换。

创建Order类:

package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author rtxtitanv
 * @version 1.0.0
 * @name com.rtxtitanv.model.Order
 * @description Order
 * @date 2021/8/18 16:45
 */
@Accessors(chain = true)
@Data
public class Order {
    private Long id;
    private String number;
    private String description;
    private User user;
}

创建OrderDTO类:

package com.rtxtitanv.model;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author rtxtitanv
 * @version 1.0.0
 * @name com.rtxtitanv.model.OrderDTO
 * @description OrderDTO
 * @date 2021/8/18 16:45
 */
@Accessors(chain = true)
@Data
public class OrderDTO {
    private Long orderId;
    private String orderNumber;
    private String orderDescription;
    private UserDTO userDTO;
}

dozer.xml中新增以下配置:

<mapping date-format="yyyy/MM/dd HH:mm:ss" map-id="order">
    <class-a>com.rtxtitanv.model.Order</class-a>
    <class-b>com.rtxtitanv.model.OrderDTO</class-b>
    <field>
        <a>id</a>
        <b>orderId</b>
    </field>
    <field>
        <a>number</a>
        <b>orderNumber</b>
    </field>
    <field>
        <a>description</a>
        <b>orderDescription</b>
    </field>
    <field>
        <a>user</a>
        <b>userDTO</b>
    </field>
</mapping>

新增以下测试方法:

@Test
void test6() {
    OrderDTO orderDTO = new OrderDTO();
    UserDTO userDTO = new UserDTO()Springboot2.x集成单节点Redis

Springboot2.x集成Redis哨兵模式

搞定全局ID生成器:SpringBoot2.x 集成百度 uidgenerator

又一神操作,SpringBoot2.x 集成百度 uidgenerator搞定全局ID

SpringBoot2.x集成分布式搜索引擎Elasticsearch

SpringBoot2.x集成knife4j