国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

在 Spring Boot 應(yīng)用程序中將 MapStruct 與 Lombok 結(jié)合使用的方法

這篇具有很好參考價(jià)值的文章主要介紹了在 Spring Boot 應(yīng)用程序中將 MapStruct 與 Lombok 結(jié)合使用的方法。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

在本文中,您將找到有關(guān)如何高效使用 MapStruct、Lombok 和 Spring Boot 的代碼示例和說明。

介紹

????????當(dāng)您實(shí)現(xiàn)任何規(guī)模的服務(wù)時(shí),您通常需要將數(shù)據(jù)從一種結(jié)構(gòu)移動(dòng)到另一種結(jié)構(gòu)。通常,這是在不同邏輯層使用的相同數(shù)據(jù) - 在業(yè)務(wù)邏輯、數(shù)據(jù)庫(kù)級(jí)別或用于傳輸?shù)角岸藨?yīng)用程序的控制器級(jí)別。

????????要傳輸這些數(shù)據(jù),您必須重復(fù)大量樣板文件。真累。我想提請(qǐng)您注意一個(gè)可以幫助您節(jié)省精力的圖書館。認(rèn)識(shí) MapStructure!

使用這個(gè)庫(kù),您只能指定結(jié)構(gòu)映射方案。其實(shí)現(xiàn)將由圖書館自行收集。

在哪里可以找到它

最新版本可以在Maven 中央存儲(chǔ)庫(kù)中找到。
您可以將其添加到您的pom.xml:

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.5.5.Final</version>
</dependency>

您需要在插件中添加注釋處理器:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <annotationProcessorPaths>
            <path>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>1.5.5.Final</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

如果您使用 Gradle,可以將其添加到您的build.gradle

implementation "org.mapstruct:mapstruct:${mapstructVersion}"
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"

怎么運(yùn)行的

我們以一些數(shù)據(jù)類為例:

public class CatEntity {
? ? private Long id;
    private String name;
    private String color;
    // getters and setters
}
 
public class CatDto {
    private Long id;
    private String name;
    private String color;
    // getters and setters
}

這就是我們需要為其實(shí)現(xiàn)編寫的所有代碼:

@Mapper
public interface CatMapper {

? ? CatEntity toEntity(CatDto dto);

    CatDto toDto(CatEntity entity);
}

該接口的實(shí)現(xiàn)將由 MapStruct 本身創(chuàng)建:

@Generated
public class?CatMapperImpl implements?CatMapper {

    @Override
? ??public?CatEntity toEntity(CatDto dto) {
        if?( dto == null?) {
            return null;
        }

        CatEntity catEntity = new?CatEntity();

        catEntity.setId( dto.getId() );
        catEntity.setName( dto.getName() );
        catEntity.setColor( dto.getColor() );

        return?catEntity;
    }

    @Override
? ??public?CatDto toDto(CatEntity entity) {
        if?( entity == null?) {
            return null;
        }

        CatDto catDto = new?CatDto();

        catDto.setId( entity.getId() );
        catDto.setName( entity.getName() );
        catDto.setColor( entity.getColor() );

        return?catDto;
    }
}

如何將 MapStruct 與 Java 記錄一起使用

在Java 14中,添加了記錄類。MapStruct 也可以處理它們:

public class CatEntity {
 ? ?private Long id;
 ? ?private String name;
 ? ?private String color;
 ? ?// getters and setters
}
 
public record CatRecord(
 ? ?Long id,
 ? ?String name,
 ? ?String color
) {
}

如果我們創(chuàng)建一個(gè)映射接口:

@Mapper
public interface?CatRecordMapper {

    CatEntity toEntity(CatRecord record);

    CatRecord toRecord(CatEntity entity);
}

然后 Mapstruct 將生成如下實(shí)現(xiàn):

@Generated
public class?CatRecordMapperImpl implements?CatRecordMapper {

    @Override
? ??public?CatEntity toEntity(CatRecord record) {
        if?( record == null?) {
            return null;
        }

        CatEntity catEntity = new?CatEntity();

        catEntity.setId( record.id() );
        catEntity.setName( record.name() );
        catEntity.setColor( record.color() );

        return?catEntity;
    }

    @Override
? ??public?CatRecord toRecord(CatEntity entity) {
        if?( entity == null?) {
            return null;
        }

        Long id = null;
        String name = null;
        String color = null;

        id = entity.getId();
        name = entity.getName();
        color = entity.getColor();

        CatRecord catRecord = new?CatRecord( id, name, color );

        return?catRecord;
    }
}

如何將 MapStruct 與 Project Lombok 結(jié)合使用

????????在Java世界中,有一個(gè)廣為人知的大型庫(kù)——Project Lombok。它還可以減少開發(fā)人員必須編寫的樣板代碼。有關(guān)該庫(kù)的更多詳細(xì)信息,您可以在官方網(wǎng)站上找到。
要將此庫(kù)添加到您的項(xiàng)目中,您需要將其添加到 pom.xml 中:

<dependency>
????????<groupId>org.projectlombok</groupId>
????????<artifactId>lombok</artifactId>
????????<version>1.18.28</version>
????????<scope>provided</scope>
</dependency>

?而且,您需要將其添加到注釋處理器中:

<annotationProcessorPaths>
????<path>
????????<groupId>org.projectlombok</groupId>
????????<artifactId>lombok</artifactId>
????????<version>1.18.28</version>
????</path>
</annotationProcessorPaths>

對(duì)于 Gradle 來說,稍微簡(jiǎn)單一些。只需將其添加到 ?build.gradle

implementation 'org.projectlombok:lombok:1.18.28'
annotationProcessor "org.projectlombok:lombok:1.18.28"

并且邁出了重要的一步!要將 Project Lombok 與 MapStruct 集成,您需要添加綁定庫(kù):

annotationProcessor 'org.projectlombok:lombok-mapstruct-binding:0.2.0'

對(duì)于我們的示例,使用@Data注釋就足夠了,它添加了 getter 和 setter。

@Data
public class?CatDto {
    private?Long id;
    private?String name;
    private?String color;
}

@Data
public class?CatEntity {
    private?Long id;
    private?String name;
    private?String color;
}

對(duì)于同一個(gè)mapper接口,它會(huì)生成以下實(shí)現(xiàn):

public interface CatMapper {
    CatEntity toEntity(CatDto dto);
    CatDto toDto(CatEntity entity);
}

@Generated
public class?CatMapperImpl implements?CatMapper {

    @Override
? ??public?CatEntity toEntity(CatDto dto) {
        if?( dto == null?) {
            return null;
        }

        CatEntity catEntity = new?CatEntity();

        catEntity.setId( dto.getId() );
        catEntity.setName( dto.getName() );
        catEntity.setColor( dto.getColor() );

        return?catEntity;
    }

    @Override
? ??public?CatDto toDto(CatEntity entity) {
        if?( entity == null?) {
            return null;
        }

        CatDto catDto = new?CatDto();

        catDto.setId( entity.getId() );
        catDto.setName( entity.getName() );
        catDto.setColor( entity.getColor() );

        return?catDto;
    }
}

如何將 Spring Boot 邏輯添加到 Mapper 中

有時(shí)需要從Spring的bean中檢索一些字段。假設(shè)我們不在數(shù)據(jù)庫(kù)中存儲(chǔ)權(quán)重信息,這在 Entity 中是無法訪問的。相反,我們有一些 Spring 的服務(wù)來提供此信息。

@Data
public class?CatDto {
    private?Long id;
    private?String name;
    private?String color;
    private?Integer weight;
}


@Data
public class?CatEntity {
    private?Long id;
    private?String name;
    private?String color;
}
And there is service that provides weight information:

@Service
public class?CatWeightProvider {

    public?Integer getWeight(String name) {
        // some logic for retrieving weight info
? ? ? ??return?5;
    }
}

要使用此 bean 檢索映射器接口內(nèi)的權(quán)重信息,應(yīng)將其替換為具有描述所有附加邏輯的方法的抽象類。

@Mapper(componentModel = "spring")
public abstract class?CatMapper {

    @Autowired
    private CatWeightProvider provider;

    @Mapping(target = "weight", source = "entity.name", qualifiedByName = "retrieveWeight")
    public abstract CatDto toDto(CatEntity entity);

    @Named("retrieveWeight")
    protected Integer retrieveWeight(String name) {
        return provider.getWeight(name);
    }
}

在這種情況下,MapStruct將生成這個(gè)抽象類的實(shí)現(xiàn):

@Generated
@Component
public class?CatMapperImpl extends?CatMapper {

    @Override
? ??public?CatDto toDto(CatEntity entity) {
        if?( entity == null?) {
            return null;
        }

        CatDto catDto = new?CatDto();

        catDto.setWeight( retrieveWeight( entity.getName() ) );
        catDto.setId( entity.getId() );
        catDto.setName( entity.getName() );
        catDto.setColor( entity.getColor() );

        return?catDto;
    }
}

如何忽略字段并映射具有不同名稱的字段

例如,數(shù)據(jù)庫(kù)實(shí)體中的字段和 dto 中的字段具有不同的名稱。?

例如,我們需要忽略 dto 層中的字段權(quán)重。

@Data
public class?CatDto {
    private?String name;
    private?String color;
    private?Integer weight;
}

@Data
public class?CatEntity {
    private?Long idInDatabase;
    private?String nameInDatabase;
    private?String colorInDatabase;
}

可以通過以下參數(shù)來完成:

@Mapper
public interface?CatMapper {

    @Mapping(target = "weight", ignore = true)
    @Mapping(target = "name", source = "entity.nameInDatabase")
    @Mapping(target = "color", source = "entity.colorInDatabase")
    CatDto toDto(CatEntity entity);
}

因此,MapStruct 的實(shí)現(xiàn)將是:

@Generated
public class?CatMapperImpl implements?CatMapper {

    @Override
? ??public?CatDto toDto(CatEntity entity) {
        if?( entity == null?) {
            return null;
        }

        CatDto catDto = new?CatDto();

        catDto.setName( entity.getNameInDatabase() );
        catDto.setColor( entity.getColorInDatabase() );

        return?catDto;
    }
}

結(jié)論

????????我們已經(jīng)介紹了開發(fā)多層應(yīng)用程序時(shí)出現(xiàn)的最流行的場(chǎng)景。因此,Project Lombok 和 MapStruct 庫(kù)的結(jié)合可以顯著節(jié)省開發(fā)人員在樣板文件上的時(shí)間和精力。
感謝您的關(guān)注!文章來源地址http://www.zghlxwxcb.cn/news/detail-632794.html

到了這里,關(guān)于在 Spring Boot 應(yīng)用程序中將 MapStruct 與 Lombok 結(jié)合使用的方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【Spring Boot】數(shù)據(jù)庫(kù)持久層框架MyBatis — Spring Boot構(gòu)建MyBatis應(yīng)用程序

    Spring Boot是用于快速構(gòu)建Spring應(yīng)用程序的框架。MyBatis是一種Java持久化框架,可以幫助開發(fā)人員輕松地管理數(shù)據(jù)庫(kù)。將Spring Boot與MyBatis結(jié)合使用可以使開發(fā)人員更容易地創(chuàng)建和管理數(shù)據(jù)庫(kù)應(yīng)用程序。 以下是使用Spring Boot構(gòu)建MyBatis應(yīng)用程序的步驟: 添加MyBatis依賴項(xiàng):在項(xiàng)目的

    2024年02月10日
    瀏覽(27)
  • 快速入門:使用 Spring Boot 構(gòu)建 Web 應(yīng)用程序

    快速入門:使用 Spring Boot 構(gòu)建 Web 應(yīng)用程序

    本文將討論以下主題: 安裝 Java JDK、Gradle 或 Maven 和 Eclipse 或 IntelliJ IDEA 創(chuàng)建一個(gè)新的 Spring Boot 項(xiàng)目 運(yùn)行 Spring Boot 應(yīng)用程序 編寫一個(gè)簡(jiǎn)單的 Web 應(yīng)用程序 打包應(yīng)用程序以用于生產(chǎn)環(huán)境 通過這些主題,您將能夠開始使用 Spring Boot 并創(chuàng)建自己的 Web 應(yīng)用程序。 Spring Boot是一個(gè)

    2024年02月07日
    瀏覽(70)
  • Spring Boot應(yīng)用程序如何配置 HTTPS 訪問方式

    在 Spring Boot 應(yīng)用程序中配置 HTTPS 訪問方式可以根據(jù)你是否擁有由受信任的證書頒發(fā)機(jī)構(gòu)(CA)簽發(fā)的證書來分為兩種情況:使用自簽名證書和使用 CA 簽發(fā)的證書。下面我將分別介紹這兩種情況的配置方法: 使用自簽名證書 如果你還沒有有效的 SSL/TLS 證書,可以選擇生成一個(gè)

    2024年01月20日
    瀏覽(18)
  • 將 Spring Boot 應(yīng)用程序與 Amazon DocumentDB 集成

    將 Spring Boot 應(yīng)用程序與 Amazon DocumentDB 集成

    Amazon DocumentDB(與 MongoDB 兼容)是一種可擴(kuò)展、高度持久和完全托管的數(shù)據(jù)庫(kù)服務(wù),用于操作任務(wù)關(guān)鍵型 MongoDB 工作負(fù)載。在 Amazon DocumentDB 上,您可以使用相同的 MongoDB 應(yīng)用程序代碼、驅(qū)動(dòng)程序和工具來運(yùn)行、管理和擴(kuò)展工作負(fù)載,無需關(guān)心管理底層基礎(chǔ)設(shè)施。? Spring Boot?提

    2024年02月10日
    瀏覽(27)
  • 深入探討Spring Boot:實(shí)現(xiàn)一個(gè)完整的RESTful應(yīng)用程序

    在這篇博客中,我們將深入探討如何使用Spring Boot構(gòu)建一個(gè)完整的RESTful應(yīng)用程序,數(shù)據(jù)庫(kù)選擇MySQL。我們將通過實(shí)現(xiàn)一個(gè)簡(jiǎn)單的用戶管理系統(tǒng)來演示Spring Boot的強(qiáng)大功能。 首先,訪問Spring Initializr,選擇以下依賴項(xiàng): Web:用于創(chuàng)建Web應(yīng)用程序 JPA:用于訪問數(shù)據(jù)庫(kù) MySQL:用于連

    2024年02月03日
    瀏覽(18)
  • 使用Spring Boot和Docker快速部署Java應(yīng)用程序

    隨著微服務(wù)的興起,容器化技術(shù)已成為現(xiàn)代應(yīng)用程序開發(fā)和部署的關(guān)鍵部分。Docker作為一種流行的容器化解決方案,廣泛應(yīng)用于企業(yè)和開發(fā)者社區(qū)。與此同時(shí),Spring Boot作為一種優(yōu)秀的Java開發(fā)框架,大大簡(jiǎn)化了基于Spring的應(yīng)用程序開發(fā)。在本文中,我們將探討如何將Spring Bo

    2024年02月01日
    瀏覽(23)
  • “深入了解Spring Boot:構(gòu)建高效、可擴(kuò)展的Java應(yīng)用程序“

    標(biāo)題:深入了解Spring Boot:構(gòu)建高效、可擴(kuò)展的Java應(yīng)用程序 摘要:Spring Boot是一款快速構(gòu)建Java應(yīng)用程序的開發(fā)框架,它基于Spring框架,提供了一系列的自動(dòng)化配置和約定,使得開發(fā)者能夠更快速、更高效地構(gòu)建出高質(zhì)量的應(yīng)用程序。本文將深入探討Spring Boot的核心特性和使用

    2024年02月14日
    瀏覽(28)
  • “深入理解Spring Boot:構(gòu)建高效、可擴(kuò)展的Java應(yīng)用程序“

    標(biāo)題:深入理解Spring Boot:構(gòu)建高效、可擴(kuò)展的Java應(yīng)用程序 摘要:Spring Boot是一個(gè)用于構(gòu)建Java應(yīng)用程序的開源框架,它提供了一種簡(jiǎn)單且高效的方式來創(chuàng)建獨(dú)立的、生產(chǎn)級(jí)別的應(yīng)用程序。本文將深入探討Spring Boot的核心概念和特性,并通過示例代碼展示如何使用Spring Boot構(gòu)建

    2024年02月15日
    瀏覽(34)
  • Spring Boot實(shí)踐:構(gòu)建WebSocket實(shí)時(shí)通信應(yīng)用程序并創(chuàng)建訂閱端點(diǎn)

    作為一款流行的Java開發(fā)框架,Spring Boot可以輕松地集成WebSocket。WebSocket能夠?yàn)閃eb應(yīng)用程序提供實(shí)時(shí)通信功能,而Spring Boot的優(yōu)秀特性使得它可以很容易地實(shí)現(xiàn)WebSocket的集成。在本篇文章中,我們將演示如何使用Spring Boot框架來構(gòu)建一個(gè)簡(jiǎn)單的WebSocket應(yīng)用程序。 1. 創(chuàng)建Spring Boo

    2024年02月01日
    瀏覽(21)
  • “深入理解Spring Boot:構(gòu)建獨(dú)立、可擴(kuò)展的企業(yè)級(jí)應(yīng)用程序的最佳實(shí)踐“

    標(biāo)題:深入理解Spring Boot:構(gòu)建獨(dú)立、可擴(kuò)展的企業(yè)級(jí)應(yīng)用程序的最佳實(shí)踐 摘要:Spring Boot是一個(gè)強(qiáng)大的框架,可以幫助開發(fā)人員快速構(gòu)建獨(dú)立、可擴(kuò)展的企業(yè)級(jí)應(yīng)用程序。本文將深入探討Spring Boot的核心概念和最佳實(shí)踐,并通過示例代碼演示其用法。 正文: 什么是Spring Bo

    2024年02月14日
    瀏覽(32)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包