锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. JAVA
  4. 在 Spring Boot 应用程序中将 MapStruct 与 Lombok 结合使用的方法

在 Spring Boot 应用程序中将 MapStruct 与 Lombok 结合使用的方法

0
  • JAVA
  • 发布于 2024-08-14
  • 0 次阅读
黄健
黄健

本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net

在本文中,您将找到有关如何高效使用 MapStruct、Lombok 和 Spring Boot 的代码示例和说明。

介绍

        当您实现任何规模的服务时,您通常需要将数据从一种结构移动到另一种结构。通常,这是在不同逻辑层使用的相同数据 - 在业务逻辑、数据库级别或用于传输到前端应用程序的控制器级别。

        要传输这些数据,您必须重复大量样板文件。真累。我想提请您注意一个可以帮助您节省精力的图书馆。认识 MapStructure!

使用这个库,您只能指定结构映射方案。其实现将由图书馆自行收集。

在哪里可以找到它

最新版本可以在 Maven 中央存储库中找到。
您可以将其添加到您的 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}"

怎么运行的

我们以一些数据类为例:

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
}

这就是我们需要为其实现编写的所有代码:

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

该接口的实现将由 MapStruct 本身创建:

@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
) {
}

如果我们创建一个映射接口:

@Mapper
public interface CatRecordMapper {
 
    CatEntity toEntity(CatRecord record);
 
    CatRecord toRecord(CatEntity entity);
}

然后 Mapstruct 将生成如下实现:

@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 结合使用

        在 Java 世界中,有一个广为人知的大型库——Project Lombok。它还可以减少开发人员必须编写的样板代码。有关该库的更多详细信息,您可以在官方网站上找到。
要将此库添加到您的项目中,您需要将其添加到 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>

对于 Gradle 来说,稍微简单一些。只需将其添加到  build.gradle

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

并且迈出了重要的一步!要将 Project Lombok 与 MapStruct 集成,您需要添加绑定库:

@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;
}

对于我们的示例,使用@Data注释就足够了,它添加了 getter 和 setter。

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;
    }
}

对于同一个 mapper 接口,它会生成以下实现:

@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;
    }
}

如何将 Spring Boot 逻辑添加到 Mapper 中

有时需要从 Spring 的 bean 中检索一些字段。假设我们不在数据库中存储权重信息,这在 Entity 中是无法访问的。相反,我们有一些 Spring 的服务来提供此信息。

@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);
    }
}

要使用此 bean 检索映射器接口内的权重信息,应将其替换为具有描述所有附加逻辑的方法的抽象类。

@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;
    }
}

在这种情况下,MapStruct 将生成这个抽象类的实现:

@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;
}

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

例如,数据库实体中的字段和 dto 中的字段具有不同的名称。 

例如,我们需要忽略 dto 层中的字段权重。

@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);
}

可以通过以下参数来完成:

@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;
    }
}

因此,MapStruct 的实现将是:

@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;
    }
}

结论

        我们已经介绍了开发多层应用程序时出现的最流行的场景。因此,Project Lombok 和 MapStruct 库的结合可以显着节省开发人员在样板文件上的时间和精力。
感谢您的关注!

标签: #软件开发 1171 #JAVA 991
相关文章

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

SpringBoot整合异步任务执行 2024-10-08 11:24

同步任务: 同步任务是在单线程中按顺序执行,每次只有一个任务在执行,不会引发线程安全和数据一致性等 并发问题 同步任务需要等待任务执行完成后才能执行下一个任务,无法同时处理多个任务,响应慢,影响用 户体验 异步任务: 异步任务是在多线程中同时执行,多个任务可以并发执行,同时处理多个请求,响应快,资源

springboot kafka多数据源,通过配置动态加载发送者和消费者 2024-10-08 11:24

前言 最近做项目,需要支持kafka多数据源,实际上我们也可以通过代码固定写死多套kafka集群逻辑,但是如果需要不修改代码扩展呢,因为kafka本身不处理额外逻辑,只是起到削峰,和数据的传递,那么就需要对架构做一定的设计了。 准备test kafka本身非常容易上手,如果我们需要单元测试,引入ja

SpringBoot 集成 Redis 2024-10-08 11:24

一:SpringBoot 集成 Redis ①Redis是一个 NoSQL(not only)数据库, 常作用缓存 Cache 使用。 ②Redis是一个中间件、是一个独立的服务器;常用的数据类型: string , hash ,set ,zset , list ③通过Redis客户端可以使用多种语

SpringBoot整合QQ邮箱 2024-10-08 11:24

SpringBoot可以通过导入依赖的方式集成多种技术,这当然少不了我们常用的邮箱,现在本章演示SpringBoot整合QQ邮箱发送邮件…. 下面按步骤进行: 1.获取QQ邮箱授权码 1.1 登录QQ邮箱 1.2 开启SMTP服务 找到下图中的SMTP服务区域,如果当前账号未开启的话自己手动开启。

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.