SpringBoot?整合mapstruct的實現(xiàn)步驟
前言
在項目中,如果我們要遵循分層領域模型規(guī)約: 話,肯定避免不了在DTO、VO、BO、AO、VO、Query等實體的轉(zhuǎn)換,我們通常有幾種做法:
手動一個個字段的賦值通過反序列化的手段,必須先轉(zhuǎn)成JSON字符串,再轉(zhuǎn)回來使用Spring的BeanUtils,提供的克隆方法
上面三種方式我們應該都使用過,但是我們今天介紹的主角是mapstruct,我們接下來見到介紹下它,以及為什么選擇它。
什么是DTO、VO、BO、AO、VO、Query
這里是摘錄自《阿里巴巴Java開發(fā)規(guī)范》
- DO(Data Object):此對象與數(shù)據(jù)庫表結(jié)構(gòu)一一對應,通過 DAO 層向上傳輸數(shù)據(jù)源對象。?
- DTO(Data Transfer Object):數(shù)據(jù)傳輸對象,Service 或 Manager 向外傳輸?shù)膶ο蟆?/li>
- BO(Business Object):業(yè)務對象,由 Service 層輸出的封裝業(yè)務邏輯的對象。
- AO(ApplicationObject):應用對象,在Web層與Service層之間抽象的復用對象模型, 極為貼近展示層,復用度不高。
- VO(View Object):顯示層對象,通常是 Web 向模板渲染引擎層傳輸?shù)膶ο蟆?/li>
Query:數(shù)據(jù)查詢對象,各層接收上層的查詢請求。注意超過 2 個參數(shù)的查詢封裝,禁止
使用 Map 類來傳輸。
mapstruct 使用來干什么的?
通俗的來說,mapstruct就是用來做對象復制的
mapstruct 相對于BeanUtils的優(yōu)勢在哪?
- 支持復雜屬性賦值
- 效率高,在編譯時直接給你生成代碼,相當與幫你手動去一個個賦值
- 支持不同字段間的賦值,通過注解實現(xiàn)
編碼
引入依賴
項目中除了引用mapstruct本身的依賴 ,還引入了神器lombok,不用寫get set,其實這里也引了一個坑進來,相信同學應該也碰到過:
當lombok和mapstruct一起用的時候,會導致mapstruct失效?
后面會幫助大家解決這個問題。
<?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 對象轉(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>
<!-- 不是必備 只是為了懶,不用寫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);
}
編寫測試用例
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("清華大學")
.userId("ams")
.userName("AI碼師")
.age(27)
.address("合肥")
.build();
StudentDto studentDto = mainMapper.studentVo2Dto(studentVo);
System.out.println(studentDto);
}
}
運行測試用例
運行test之后,發(fā)現(xiàn)輸出內(nèi)容是空的

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

看到生成的代碼里面只是new了一個新的對象,并沒有做賦值操作。
這是為什么呢?
答案:由于mapstruct和lombok都會在編譯期為項目生成代碼,兩個如果一起用的話,就有可能導致mapstruct失效;我猜測有可能我們借助lombok生成 get set方法的原因,有可能mapstruct生成代碼之前,lombok還沒有生成get set方法,所以mapstruct也就調(diào)用不了get set 進行賦值了。
怎么解決mapstruct 失效呢?
其實我們只需要引入一個依賴就可以了
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</dependency>
重新運行下,看是不是解決了

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

mapstruct常規(guī)操作
不同字段映射
如果兩個實體中 有幾個字段命名不一致,可以使用@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的基本過程,解決了mapstruct和lombok一起使用,導致mapstruct失效的bug,另外也介紹了mapstruct的基本使用方法,后續(xù)會出更多集成指南,敬請期待!
代碼已經(jīng)上傳到碼云:https://gitee.com/lezaiclub/springboot-hyper-integration.git,歡迎白嫖
到此這篇關于SpringBoot 整合mapstruct的文章就介紹到這了,更多相關SpringBoot 整合mapstruct內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java9的JShell小工具和編譯器兩種自動優(yōu)化方法
這篇文章主要介紹了java9的JShell小工具和編譯器兩種自動優(yōu)化方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07
SpringBoot使用WebJars統(tǒng)一管理靜態(tài)資源的方法
這篇文章主要介紹了SpringBoot使用WebJars統(tǒng)一管理靜態(tài)資源的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
java如何把逗號分隔的String字符串轉(zhuǎn)int集合
這篇文章主要介紹了java實現(xiàn)把逗號分隔的String字符串轉(zhuǎn)int集合,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
ShardingSphere數(shù)據(jù)分片算法及測試實戰(zhàn)
這篇文章主要為大家介紹了ShardingSphere數(shù)據(jù)分片算法及測試實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
springBoot中myBatisPlus的使用步驟及示例代碼
MyBatis-Plus 是一個 MyBatis 的增強工具,在 Spring Boot 項目里使用它能極大提升開發(fā)效率,下面為你詳細介紹在 Spring Boot 中使用 MyBatis-Plus 的步驟以及示例代碼,感興趣的朋友一起看看吧2025-03-03
使用Spring Validation實現(xiàn)數(shù)據(jù)校驗的代碼詳解
在現(xiàn)代Web應用開發(fā)中,數(shù)據(jù)校驗是不可忽視的重要環(huán)節(jié),Spring提供了強大的數(shù)據(jù)校驗框架——Spring Validation,可以有效提升數(shù)據(jù)輸入的安全性與應用的穩(wěn)定性,本文將介紹如何使用Spring Validation進行數(shù)據(jù)校驗,幫助您深入理解和靈活應用這一技術2024-11-11

