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

SpringBoot使用MapStruct生成映射代碼的示例詳解

 更新時(shí)間:2024年11月26日 11:06:01   作者:鵬阿鵬  
MapStruct 是一個(gè)用于 Java 的代碼生成器,專門用于生成類型安全的 bean 映射代碼,它通過注解處理器在編譯時(shí)生成映射代碼,從而避免了運(yùn)行時(shí)的性能開銷和潛在的錯(cuò)誤,本文給大家介紹了SpringBoot使用MapStruct生成映射代碼的示例,需要的朋友可以參考下

定義

MapStruct 是一個(gè)用于 Java 的代碼生成器,專門用于生成類型安全的 bean 映射代碼。它通過注解處理器在編譯時(shí)生成映射代碼,從而避免了運(yùn)行時(shí)的性能開銷和潛在的錯(cuò)誤。

MapStruct 的主要目標(biāo)是簡化和加速 Java 對象之間的轉(zhuǎn)換,特別是當(dāng)這些對象具有相似的結(jié)構(gòu)時(shí)。

相關(guān)概念

  • Mapper 接口 Mapper 接口定義了對象之間的映射方法。你可以通過在接口上使用 @Mapper
    注解來標(biāo)記這個(gè)接口是一個(gè)映射器。MapStruct 會(huì)在編譯時(shí)生成這個(gè)接口的實(shí)現(xiàn)類。
  • Mapping 注解 @Mapping 注解用于定義源對象和目標(biāo)對象之間的字段映射關(guān)系。你可以在 Mapper
    接口的方法上使用這個(gè)注解來指定具體的映射規(guī)則。
  • Mappings 注解 @Mappings 注解是 @Mapping 注解的容器,用于定義多個(gè)字段映射。
  • Component Model 通過 componentModel 屬性,你可以指定生成的 Mapper 實(shí)現(xiàn)類的組件模型,例如Spring、CDI 或默認(rèn)的無組件模型。

使用示例

  1. 定義源對象和目標(biāo)對象
public class Source {
    private String name;
    private int age;
    private String address;

    // getters and setters
}

public class Target {
    private String fullName;
    private int age;
    private String location;

    // getters and setters
}
  • 定義Mapper接口
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

@Mapper
public interface SourceTargetMapper {

    SourceTargetMapper INSTANCE = Mappers.getMapper(SourceTargetMapper.class);

    @Mapping(source = "name", target = "fullName")
    @Mapping(source = "address", target = "location")
    Target sourceToTarget(Source source);

    @Mapping(source = "fullName", target = "name")
    @Mapping(source = "location", target = "address")
    Source targetToSource(Target target);
}

或者,可以使用 @Mappings 注解來包含多個(gè) @Mapping 注解。@Mappings 注解是 @Mapping 注解的容器,用于定義多個(gè)字段映射。

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

@Mapper
public interface SourceTargetMapper {

    SourceTargetMapper INSTANCE = Mappers.getMapper(SourceTargetMapper.class);

    @Mappings({
        @Mapping(source = "name", target = "fullName"),
        @Mapping(source = "address", target = "location")
    })
    Target sourceToTarget(Source source);

    @Mappings({
        @Mapping(source = "fullName", target = "name"),
        @Mapping(source = "location", target = "address")
    })
    Source targetToSource(Target target);
}

  • 主方法
public class Main {
    public static void main(String[] args) {
        Source source = new Source();
        source.setName("John Doe");
        source.setAge(30);
        source.setAddress("123 Main St");

        SourceTargetMapper mapper = SourceTargetMapper.INSTANCE;
        Target target = mapper.sourceToTarget(source);

        System.out.println("Target Full Name: " + target.getFullName());
        System.out.println("Target Age: " + target.getAge());
        System.out.println("Target Location: " + target.getLocation());
    }
}

與Spring集成

通過設(shè)置 componentModel = "spring",你可以將生成的 Mapper 實(shí)現(xiàn)類作為 Spring 組件進(jìn)行管理,從而在 Spring 容器中進(jìn)行依賴注入。

@Mapper(componentModel = "spring")
public interface SourceTargetMapper {
    // 映射方法
}

使用如下

@Service
public class SomeService {

    private final SourceTargetMapper sourceTargetMapper;

    @Autowired
    public SomeService(SourceTargetMapper sourceTargetMapper) {
        this.sourceTargetMapper = sourceTargetMapper;
    }

    // 使用 sourceTargetMapper 進(jìn)行對象轉(zhuǎn)換
}

表達(dá)式功能

可以使用 expression 屬性在 @Mapping 注解中指定自定義的表達(dá)式。

示例場景:假設(shè)我們有一個(gè)場景,需要將一個(gè)包含日期字符串的源對象轉(zhuǎn)換為目標(biāo)對象,并且需要將日期字符串解析為 java.util.Date 對象。我們將使用 java.text.SimpleDateFormat 類來解析日期字符串。

public class Source {
    private String dateString;

    // getters and setters
}

public class Target {
    private Date date;

    // getters and setters
}

定義日期解析工具類

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateParser {
    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static Date parse(String dateString) throws ParseException {
        return DATE_FORMAT.parse(dateString);
    }
}

定義Mapper接口,注意注解中的imports和expression

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

@Mapper(componentModel = "spring", imports = {DateParser.class, ParseException.class})
public interface SourceTargetMapper {

    SourceTargetMapper INSTANCE = Mappers.getMapper(SourceTargetMapper.class);

    @Mappings({
        @Mapping(target = "date", expression = "java(DateParser.parse(source.getDateString()))")
    })
    Target sourceToTarget(Source source) throws ParseException;
}

在這個(gè) Mapper接口中,使用了 imports 屬性導(dǎo)入了 DateParser 和 ParseException 類。然后在 @Mapping 注解的 expression 屬性中,通過 DateParser.parse(source.getDateString()) 來將 dateString 轉(zhuǎn)換為 Date 對象。

public class Main {
    public static void main(String[] args) {
        Source source = new Source();
        source.setDateString("2024-11-25");

        SourceTargetMapper mapper = SourceTargetMapper.INSTANCE;
        try {
            Target target = mapper.sourceToTarget(source);
            System.out.println("Target Date: " + target.getDate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

在這個(gè)示例中,創(chuàng)建了一個(gè)包含日期字符串的 Source 對象,并使用 SourceTargetMapper 將其轉(zhuǎn)換為 Target 對象。轉(zhuǎn)換過程中,DateParser 類的 parse 方法被調(diào)用,將日期字符串解析為 Date 對象。

依賴:

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.5.5.Final</version> <!-- 請根據(jù)需要替換為最新版本 -->
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.5.5.Final</version> <!-- 請根據(jù)需要替換為最新版本 -->
    <scope>provided</scope>
</dependency>

以上就是SpringBoot使用MapStruct生成映射代碼的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot MapStruct生成映射的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java logback日志的簡單使用

    Java logback日志的簡單使用

    這篇文章主要介紹了Java logback日志的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • SpringBoot中的YAML配置文件和日志詳解

    SpringBoot中的YAML配置文件和日志詳解

    這篇文章主要介紹了SpringBoot中的YAML配置文件和日志的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-12-12
  • java如何利用FastJSON、Gson、Jackson三種Json格式工具自定義時(shí)間序列化

    java如何利用FastJSON、Gson、Jackson三種Json格式工具自定義時(shí)間序列化

    本篇文章主要介紹了java如何利用FastJSON、Gson、Jackson三種Json格式工具自定義時(shí)間序列化,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-08-08
  • 利用session實(shí)現(xiàn)簡單購物車功能

    利用session實(shí)現(xiàn)簡單購物車功能

    這篇文章主要為大家詳細(xì)介紹了利用session實(shí)現(xiàn)簡單購物車功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • idea項(xiàng)目中target文件提示拒絕訪問的解決

    idea項(xiàng)目中target文件提示拒絕訪問的解決

    這篇文章主要介紹了idea項(xiàng)目中target文件提示拒絕訪問的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 詳解springmvc 中controller與jsp傳值

    詳解springmvc 中controller與jsp傳值

    本篇文章主要介紹了springmvc 中controller與jsp傳值,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • mybatis動(dòng)態(tài)SQL?if的test寫法及規(guī)則詳解

    mybatis動(dòng)態(tài)SQL?if的test寫法及規(guī)則詳解

    這篇文章主要介紹了mybatis動(dòng)態(tài)SQL?if的test寫法及規(guī)則詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring boot實(shí)現(xiàn)熱部署的兩種方式詳解

    Spring boot實(shí)現(xiàn)熱部署的兩種方式詳解

    這篇文章主要介紹了Spring boot實(shí)現(xiàn)熱部署的兩種方式,這兩種方法分別是使用 Spring Loaded和使用spring-boot-devtools進(jìn)行熱部署,文中給出了詳細(xì)示例代碼和介紹,需要的朋友可以參考學(xué)習(xí),下面來一起看看吧。
    2017-04-04
  • Javaweb 500 服務(wù)器內(nèi)部錯(cuò)誤的解決

    Javaweb 500 服務(wù)器內(nèi)部錯(cuò)誤的解決

    這篇文章主要介紹了Javaweb 500 服務(wù)器內(nèi)部錯(cuò)誤的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • java通過Jsoup爬取網(wǎng)頁過程詳解

    java通過Jsoup爬取網(wǎng)頁過程詳解

    這篇文章主要介紹了java通過Jsoup爬取網(wǎng)頁過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09

最新評論

易门县| 抚远县| 南郑县| 土默特左旗| 抚宁县| 宣城市| 新源县| 甘谷县| 德令哈市| 玛多县| 巨野县| 温州市| 迭部县| 永登县| 精河县| 永兴县| 杭州市| 稻城县| 高邑县| 贵港市| 文水县| 赤水市| 锡林浩特市| 泗阳县| 屏南县| 罗源县| 巩义市| 伊川县| 雷山县| 安泽县| 宁武县| 固始县| 喀什市| 周宁县| 汪清县| 永清县| 修水县| 建湖县| 玉田县| 昆明市| 鄢陵县|