最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合Dozer映射框架流程詳解

 更新時間:2022年07月06日 10:06:08   作者:華仔仔coding  
dozer是用來兩個對象之間屬性轉換的工具,有了這個工具之后,我們將一個對象的所有屬性值轉給另一個對象時,就不需要再去寫重復的set和get方法了,下面介紹下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中為什么this可以調用當前實例

    Java中為什么this可以調用當前實例

    本文主要介紹了為什么可以通過this關鍵字訪問到當前對象呢,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • SpringBoot?2.x整合Log4j2日志的詳細步驟

    SpringBoot?2.x整合Log4j2日志的詳細步驟

    log4j2優(yōu)越的性能其原因在于log4j2使用了LMAX,一個無鎖的線程間通信庫代替了,logback和log4j之前的隊列,并發(fā)性能大大提升,下面這篇文章主要給大家介紹了關于SpringBoot?2.x整合Log4j2日志的相關資料,需要的朋友可以參考下
    2022-10-10
  • Java中的@Builder注解問題詳解

    Java中的@Builder注解問題詳解

    這篇文章主要介紹了Java中的@Builder注解詳解,@Builder 注解的其中一個大坑會導致默認值失效,這是使用此注解出現(xiàn)的一個問題,總的來說,不推薦再使用 @Builder 注解,接下來講重點介紹其原因和替代方案,需要的朋友可以參考下
    2023-10-10
  • SpringBoot啟動時加載指定方法的方式小結

    SpringBoot啟動時加載指定方法的方式小結

    本文主要給大家介紹了Spring Boot項目啟動時加載指定方法都有哪些方式的,文中給大家介紹了五種常用的方式,有詳細的代碼示例,具有一定的參考價值,需要的朋友可以參考下
    2023-08-08
  • JAVA-NIO之Socket/ServerSocket Channel(詳解)

    JAVA-NIO之Socket/ServerSocket Channel(詳解)

    下面小編就為大家?guī)硪黄狫AVA-NIO之Socket/ServerSocket Channel(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • java中的static{}塊的實例詳解

    java中的static{}塊的實例詳解

    這篇文章主要介紹了java中的static{}塊的實例詳解的相關資料,這里提供實例來幫助大家理解該如何使用static塊,需要的朋友可以參考下
    2017-08-08
  • Java實現(xiàn)斷點續(xù)傳功能的示例代碼

    Java實現(xiàn)斷點續(xù)傳功能的示例代碼

    這篇文章主要為大家詳細介紹了如何利用Java語言實現(xiàn)網(wǎng)絡資源的斷點續(xù)傳功能,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的可以了解一下
    2022-10-10
  • Spring配置文件超詳細講解

    Spring配置文件超詳細講解

    這篇文章主要介紹了Spring配置文件超的相關資料,配置文件是一種存放應用程序或系統(tǒng)配置的文件,允許開發(fā)者和用戶在不修改代碼的情況下調整應用程序的行為和性能,
    2025-03-03
  • java常用工具類 XML工具類、數(shù)據(jù)驗證工具類

    java常用工具類 XML工具類、數(shù)據(jù)驗證工具類

    這篇文章主要為大家詳細介紹了java常用工具類,包括XML工具類、數(shù)據(jù)驗證工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • seata的部署和集成詳細介紹

    seata的部署和集成詳細介紹

    這篇文章主要介紹了Java seata的部署和集成,文章中有詳細的代碼示例和圖片講解,對學習seata有一定的參考價值,需要的朋友可以參考一下
    2023-04-04

最新評論

潼关县| 靖远县| 马公市| 兴城市| 闵行区| 福安市| 永德县| 同江市| 郧西县| 霸州市| 怀远县| 远安县| 剑阁县| 保定市| 迁安市| 庆安县| 松溪县| 镇平县| 固原市| 富民县| 蒙阴县| 扎赉特旗| 湄潭县| 高要市| 新乡县| 鄂托克旗| 商洛市| 聂拉木县| 丹凤县| 龙南县| 古交市| 安陆市| 平山县| 龙泉市| 道真| 临江市| 大城县| 秦安县| 柏乡县| 洪湖市| 广安市|