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

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

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

定義

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

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

相關(guān)概念

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

使用示例

  1. 定義源對象和目標對象
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 注解來包含多個 @Mapping 注解。@Mappings 注解是 @Mapping 注解的容器,用于定義多個字段映射。

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 實現(xiàn)類作為 Spring 組件進行管理,從而在 Spring 容器中進行依賴注入。

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

使用如下

@Service
public class SomeService {

    private final SourceTargetMapper sourceTargetMapper;

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

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

表達式功能

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

示例場景:假設(shè)我們有一個場景,需要將一個包含日期字符串的源對象轉(zhuǎn)換為目標對象,并且需要將日期字符串解析為 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;
}

在這個 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();
        }
    }
}

在這個示例中,創(chuàng)建了一個包含日期字符串的 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生成映射代碼的示例詳解的詳細內(nèi)容,更多關(guān)于SpringBoot MapStruct生成映射的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java logback日志的簡單使用

    Java logback日志的簡單使用

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

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

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

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

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

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

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

    idea項目中target文件提示拒絕訪問的解決

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

    詳解springmvc 中controller與jsp傳值

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

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

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

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

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

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

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

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

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

最新評論

承德市| 年辖:市辖区| 镇原县| 元阳县| 河源市| 荆门市| 河间市| 磴口县| 南丰县| 林周县| 太仆寺旗| 门源| 卓资县| 庆城县| 四川省| 家居| 黄浦区| 怀化市| 繁昌县| 瑞金市| 大田县| 武邑县| 米泉市| 腾冲县| 德保县| 乌兰浩特市| 太原市| 揭东县| 浮梁县| 普安县| 屏南县| 米林县| 仙桃市| 绥芬河市| 来宾市| 石景山区| 米脂县| 延吉市| 安化县| 名山县| 琼海市|