SpringBoot整合Dozer映射框架流程詳解
1. Dozer 介紹
Dozer 是一個 Java Bean 到 Java Bean 的映射器,它遞歸地將數(shù)據(jù)從一個對象復制到另一個對象。Dozer 是用來對兩個對象之間屬性轉換的工具,有了這個工具之后,我們將一個對象的所有屬性值轉給另一個對象時,就不需要再去寫重復的調用 set 和 get 方法。
最重要的是,Dozer 可以確保來自數(shù)據(jù)庫的內部域對象不會滲入外部表示層或外部消費者,它還可以將域對象映射到外部 API 調用,反之亦然。
2. 為什么要使用映射框架 Dozer
映射框架在分層架構中作用很大,我們可以通過封裝對特定數(shù)據(jù)對象的更改與將這些對象傳播到其他層(即外部服務數(shù)據(jù)對象、域對象、數(shù)據(jù)傳輸對象、內部服務數(shù)據(jù)對象)來創(chuàng)建抽象層。 映射框架非常適合在負責將數(shù)據(jù)從一個數(shù)據(jù)對象映射到另一個數(shù)據(jù)對象的 Mapper 類型類中使用。
對于分布式系統(tǒng)架構而言,副作用是域對象在不同系統(tǒng)之間的傳遞。那么,我們不希望內部域對象暴露在外部,也不允許外部域對象滲入我們的系統(tǒng)。
數(shù)據(jù)對象之間的映射傳統(tǒng)上是通過在對象之間復制數(shù)據(jù)的手動編碼值對象組裝器(或轉換器)來解決的。Dozer 作為一個強大、通用、靈活、可重用和可配置的開源映射框架,節(jié)省了開發(fā)人員開發(fā)某種自定義映射器框架帶來的時間成本。
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 映射框架。工程的目錄結構如下圖所示:

第一步,創(chuàng)建 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>第二步,創(chuàng)建 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/ 目錄下創(chuàng)建 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/ 目錄下創(chuàng)建 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
第六步,創(chuàng)建主啟動類 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);
}
}
到此這篇關于SpringBoot整合Dozer映射框架流程詳解的文章就介紹到這了,更多相關SpringBoot Dozer映射框架內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JAVA-NIO之Socket/ServerSocket Channel(詳解)
下面小編就為大家?guī)硪黄狫AVA-NIO之Socket/ServerSocket Channel(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
java常用工具類 XML工具類、數(shù)據(jù)驗證工具類
這篇文章主要為大家詳細介紹了java常用工具類,包括XML工具類、數(shù)據(jù)驗證工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05

