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

SpringBoot?整合mapstruct的實(shí)現(xiàn)步驟

 更新時(shí)間:2021年11月30日 10:38:08   作者:AI碼師  
這篇文章主要介紹了SpringBoot整合mapstruct,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

在項(xiàng)目中,如果我們要遵循分層領(lǐng)域模型規(guī)約: 話,肯定避免不了在DTO、VO、BO、AO、VO、Query等實(shí)體的轉(zhuǎn)換,我們通常有幾種做法:

手動(dòng)一個(gè)個(gè)字段的賦值通過(guò)反序列化的手段,必須先轉(zhuǎn)成JSON字符串,再轉(zhuǎn)回來(lái)使用Spring的BeanUtils,提供的克隆方法

上面三種方式我們應(yīng)該都使用過(guò),但是我們今天介紹的主角是mapstruct,我們接下來(lái)見(jiàn)到介紹下它,以及為什么選擇它。

什么是DTO、VO、BO、AO、VO、Query

這里是摘錄自《阿里巴巴Java開(kāi)發(fā)規(guī)范》

  • DO(Data Object):此對(duì)象與數(shù)據(jù)庫(kù)表結(jié)構(gòu)一一對(duì)應(yīng),通過(guò) DAO 層向上傳輸數(shù)據(jù)源對(duì)象。?
  • DTO(Data Transfer Object):數(shù)據(jù)傳輸對(duì)象,Service 或 Manager 向外傳輸?shù)膶?duì)象。
  • BO(Business Object):業(yè)務(wù)對(duì)象,由 Service 層輸出的封裝業(yè)務(wù)邏輯的對(duì)象。
  • AO(ApplicationObject):應(yīng)用對(duì)象,在Web層與Service層之間抽象的復(fù)用對(duì)象模型, 極為貼近展示層,復(fù)用度不高。
  • VO(View Object):顯示層對(duì)象,通常是 Web 向模板渲染引擎層傳輸?shù)膶?duì)象。

Query:數(shù)據(jù)查詢對(duì)象,各層接收上層的查詢請(qǐng)求。注意超過(guò) 2 個(gè)參數(shù)的查詢封裝,禁止

使用 Map 類(lèi)來(lái)傳輸。

mapstruct 使用來(lái)干什么的?

通俗的來(lái)說(shuō),mapstruct就是用來(lái)做對(duì)象復(fù)制的

mapstruct 相對(duì)于BeanUtils的優(yōu)勢(shì)在哪?

  • 支持復(fù)雜屬性賦值
  • 效率高,在編譯時(shí)直接給你生成代碼,相當(dāng)與幫你手動(dòng)去一個(gè)個(gè)賦值
  • 支持不同字段間的賦值,通過(guò)注解實(shí)現(xiàn)

編碼

引入依賴(lài)

項(xiàng)目中除了引用mapstruct本身的依賴(lài) ,還引入了神器lombok,不用寫(xiě)get set,其實(shí)這里也引了一個(gè)坑進(jìn)來(lái),相信同學(xué)應(yīng)該也碰到過(guò):
當(dāng)lombok和mapstruct一起用的時(shí)候,會(huì)導(dǎo)致mapstruct失效?
后面會(huì)幫助大家解決這個(gè)問(wèn)題。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ams</groupId>
    <artifactId>springboot-mapstruct</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-mapstruct</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <org.mapstruct>1.4.1.Final</org.mapstruct>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- mapStruct 對(duì)象轉(zhuǎn)換 -->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct}</version>
        </dependency>
<!--        不是必備 只是為了懶,不用寫(xiě)get set方法-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.projectlombok</groupId>-->
<!--            <artifactId>lombok-mapstruct-binding</artifactId>-->
<!--            <version>0.2.0</version>-->
<!--        </dependency>-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

創(chuàng)建 DTO、VO

StudentDto

package com.ams.springbootmapstruct.dto;

import lombok.Data;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: AI碼師
 * @date: 2021/11/27
 * @description:
 * @modifiedBy:
 * @version: 1.0
 */
@Data
public class StudentDto {
    private String userName;
    private String userId;
    private String address;
    private String school;
    private int age;
    private String email;

}

StudenVo

package com.ams.springbootmapstruct.vo;

import lombok.Builder;
import lombok.Data;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: AI碼師
 * @date: 2021/11/27
 * @description:
 * @modifiedBy:
 * @version: 1.0
 */
@Data
@Builder
public class StudentVo {
    private String userName;
    private String userId;
    private String address;
    private String school;
    private int age;
    private String emailAddress;
}

創(chuàng)建mapstruct轉(zhuǎn)換器

package com.ams.springbootmapstruct.mapper;

import com.ams.springbootmapstruct.dto.StudentDto;
import com.ams.springbootmapstruct.vo.StudentVo;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface MainMapper {
    StudentDto studentVo2Dto(StudentVo vo);
}

編寫(xiě)測(cè)試用例

package com.ams.springbootmapstruct;

import com.ams.springbootmapstruct.dto.StudentDto;
import com.ams.springbootmapstruct.mapper.MainMapper;
import com.ams.springbootmapstruct.vo.StudentVo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootMapstructApplicationTests {
    @Autowired
    private MainMapper mainMapper;

    @Test
    void testSimpleMap() {
        StudentVo studentVo = StudentVo.builder()
                .school("清華大學(xué)")
                .userId("ams")
                .userName("AI碼師")
                .age(27)
                .address("合肥")
                .build();
        StudentDto studentDto = mainMapper.studentVo2Dto(studentVo);
        System.out.println(studentDto);
    }
}

運(yùn)行測(cè)試用例

運(yùn)行test之后,發(fā)現(xiàn)輸出內(nèi)容是空的

在這里插入圖片描述

這是怎么回事呢,我們看下MainMapper生成的代碼是什么樣的?

在這里插入圖片描述

看到生成的代碼里面只是new了一個(gè)新的對(duì)象,并沒(méi)有做賦值操作。

這是為什么呢?
答案:由于mapstruct和lombok都會(huì)在編譯期為項(xiàng)目生成代碼,兩個(gè)如果一起用的話,就有可能導(dǎo)致mapstruct失效;我猜測(cè)有可能我們借助lombok生成 get set方法的原因,有可能mapstruct生成代碼之前,lombok還沒(méi)有生成get set方法,所以mapstruct也就調(diào)用不了get set 進(jìn)行賦值了。

怎么解決mapstruct 失效呢?

其實(shí)我們只需要引入一個(gè)依賴(lài)就可以了

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-mapstruct-binding</artifactId>
            <version>0.2.0</version>
        </dependency>

重新運(yùn)行下,看是不是解決了

在這里插入圖片描述

再看下生成的代碼,發(fā)現(xiàn)它已經(jīng)調(diào)用set方法賦值了

在這里插入圖片描述

mapstruct常規(guī)操作

不同字段映射

如果兩個(gè)實(shí)體中 有幾個(gè)字段命名不一致,可以使用@Mapping 解決
現(xiàn)在studenVo和studenDto 有email 和emailAddress 字段不一致,可以使用如下方式解決

package com.ams.springbootmapstruct.mapper;

import com.ams.springbootmapstruct.dto.StudentDto;
import com.ams.springbootmapstruct.vo.StudentVo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface MainMapper {
    @Mapping(source = "emailAddress", target = "email")
    StudentDto studentVo2Dto(StudentVo vo);
}

LIST轉(zhuǎn)換

package com.ams.springbootmapstruct.mapper;

import com.ams.springbootmapstruct.dto.StudentDto;
import com.ams.springbootmapstruct.vo.StudentVo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import java.util.List;

@Mapper(componentModel = "spring")
public interface MainMapper {
    @Mapping(source = "emailAddress", target = "email")
    StudentDto studentVo2Dto(StudentVo vo);
    List<StudentDto> studentListVo2Dto(List<StudentVo> vo);
}

總結(jié)

本文整理了SpringBoot集成mapstruct的基本過(guò)程,解決了mapstruct和lombok一起使用,導(dǎo)致mapstruct失效的bug,另外也介紹了mapstruct的基本使用方法,后續(xù)會(huì)出更多集成指南,敬請(qǐng)期待!

代碼已經(jīng)上傳到碼云:https://gitee.com/lezaiclub/springboot-hyper-integration.git,歡迎白嫖

到此這篇關(guān)于SpringBoot 整合mapstruct的文章就介紹到這了,更多相關(guān)SpringBoot 整合mapstruct內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java9的JShell小工具和編譯器兩種自動(dòng)優(yōu)化方法

    java9的JShell小工具和編譯器兩種自動(dòng)優(yōu)化方法

    這篇文章主要介紹了java9的JShell小工具和編譯器兩種自動(dòng)優(yōu)化方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Java webSerivce的使用看完你就明白了

    Java webSerivce的使用看完你就明白了

    因?yàn)榍岸螘r(shí)間,需要使用到webService來(lái)調(diào)用公司的其他系統(tǒng)api接口,但是請(qǐng)求方式和我熟知的http請(qǐng)求不一樣,是基于soap協(xié)議來(lái)傳輸xml數(shù)據(jù)格式,請(qǐng)求的參數(shù)極其復(fù)雜,需要封裝多層xml數(shù)據(jù)格式,并且我不知道對(duì)方的api接口是什么語(yǔ)言,甚至不知道他們存在于什么平臺(tái)
    2022-03-03
  • SpringBoot使用WebJars統(tǒng)一管理靜態(tài)資源的方法

    SpringBoot使用WebJars統(tǒng)一管理靜態(tài)資源的方法

    這篇文章主要介紹了SpringBoot使用WebJars統(tǒng)一管理靜態(tài)資源的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • SpringBoot?自定義注解之脫敏注解詳解

    SpringBoot?自定義注解之脫敏注解詳解

    這篇文章主要介紹了SpringBoot?自定義注解之脫敏注解詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 深入理解HashMap各個(gè)方法的源碼

    深入理解HashMap各個(gè)方法的源碼

    這篇文章主要介紹了深入理解HashMap各個(gè)方法的源碼,HashMap初始容量不能為負(fù)數(shù),若初始容量大于最大容量,則讓它等于最大容量,負(fù)載因子必須大于0,并且傳入的initialCapacity不是HashMap的容量大小,需要的朋友可以參考下
    2023-12-12
  • java如何把逗號(hào)分隔的String字符串轉(zhuǎn)int集合

    java如何把逗號(hào)分隔的String字符串轉(zhuǎn)int集合

    這篇文章主要介紹了java實(shí)現(xiàn)把逗號(hào)分隔的String字符串轉(zhuǎn)int集合,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • ShardingSphere數(shù)據(jù)分片算法及測(cè)試實(shí)戰(zhàn)

    ShardingSphere數(shù)據(jù)分片算法及測(cè)試實(shí)戰(zhàn)

    這篇文章主要為大家介紹了ShardingSphere數(shù)據(jù)分片算法及測(cè)試實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Java?C++題解eetcode940不同的子序列?II

    Java?C++題解eetcode940不同的子序列?II

    這篇文章主要為大家介紹了Java?C++題解eetcode940不同的子序列?II實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • springBoot中myBatisPlus的使用步驟及示例代碼

    springBoot中myBatisPlus的使用步驟及示例代碼

    MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在 Spring Boot 項(xiàng)目里使用它能極大提升開(kāi)發(fā)效率,下面為你詳細(xì)介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼,感興趣的朋友一起看看吧
    2025-03-03
  • 使用Spring Validation實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的代碼詳解

    使用Spring Validation實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的代碼詳解

    在現(xiàn)代Web應(yīng)用開(kāi)發(fā)中,數(shù)據(jù)校驗(yàn)是不可忽視的重要環(huán)節(jié),Spring提供了強(qiáng)大的數(shù)據(jù)校驗(yàn)框架——Spring Validation,可以有效提升數(shù)據(jù)輸入的安全性與應(yīng)用的穩(wěn)定性,本文將介紹如何使用Spring Validation進(jìn)行數(shù)據(jù)校驗(yàn),幫助您深入理解和靈活應(yīng)用這一技術(shù)
    2024-11-11

最新評(píng)論

奇台县| 海伦市| 古浪县| 阿城市| 集安市| 海城市| 佳木斯市| 和平区| 深圳市| 花莲市| 台南县| 南京市| 墨竹工卡县| 庄浪县| 颍上县| 政和县| 长宁区| 仁寿县| 天镇县| 长葛市| 邳州市| 黄陵县| 嘉祥县| 疏勒县| 新野县| 阿克| 辽宁省| 龙游县| 虞城县| 永善县| 涞源县| 嘉义市| 东阿县| 平武县| 孟州市| 始兴县| 鹤庆县| 南乐县| 安平县| 容城县| 蕉岭县|