Home  >  Article  >  Java  >  How SpringBoot integrates the Dozer mapping framework

How SpringBoot integrates the Dozer mapping framework

WBOY
WBOYforward
2023-05-11 21:58:04840browse

1. Introduction to Dozer

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another. Dozer is a tool used to convert attributes between two objects. With this tool, when we transfer all attribute values ​​​​of one object to another object, we no longer need to write repeated calls to the set and get methods.

Most importantly, Dozer ensures that internal domain objects from the database do not leak into the external presentation layer or external consumers, and it can also map domain objects to external API calls and vice versa.

2. Why use the mapping framework Dozer

The mapping framework plays a great role in the layered architecture. We can encapsulate changes to specific data objects and propagate these objects to other layers ( That is, external service data objects, domain objects, data transfer objects, internal service data objects) to create abstraction layers. The mapping framework is ideal for use in Mapper type classes that are responsible for mapping data from one data object to another.

For distributed system architecture, a side effect is the transfer of domain objects between different systems. Well, we don't want internal domain objects to be exposed to the outside, nor do we want external domain objects to infiltrate our system.

Mapping between data objects has traditionally been solved through hand-coded value object assemblers (or converters) that copy data between objects. As a powerful, versatile, flexible, reusable and configurable open source mapping framework, Dozer saves developers the time and cost of developing a custom mapper framework.

3. Use of Dozer mapping framework

Dozer’s maven coordinates:

<dependency>
    <groupId>com.github.dozermapper</groupId>
    <artifactId>dozer-core</artifactId>
    <version>6.5.0</version>
</dependency>

In order to simplify usage, Dozer also provides a starter, whose maven coordinates are:

<dependency>
    <groupId>com.github.dozermapper</groupId>
    <artifactId>dozer-spring-boot-starter</artifactId>
    <version>6.5.0</version>
</dependency>

Let’s start using the dozer mapping framework in the springboot project. The directory structure of the project is as shown below:

How SpringBoot integrates the Dozer mapping framework

The first step is to create the maven project dozer_demo and configure the pom.xml file

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

The second step is to create UserDTO and UserEntity

UserDTO class

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 class

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;
}

The third step is to create dozer’s global configuration file global.dozer.xml# in the resources/dozer/ directory. ##

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

The fourth step is to create the dozer mapping file biz.dozer.xml in the resources/dozer/ directory

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

The fifth step is to write the application.yml file

dozer:

mappingFiles:
- classpath:dozer/global.dozer.xml
- classpath:dozer/biz.dozer.xml

The sixth step is to create the master Start the class 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);
    }
}

The seventh step is to write the unit test 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);
    }
}

The above is the detailed content of How SpringBoot integrates the Dozer mapping framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete