Springboot2.X 集成MapStruct相关问题

一、MapStruct使用

MapStruct是一种类型安全的bean映射类生成java注释处理器。我们要做的就是定义一个映射器接口,声明任何必需的映射方法。在编译的过程中,MapStruct注解处理器(mapstruct-processor会在target/generated-sources/annotations下会生成@Mapper接口的实现类。

使用mapstruct需要mapstruct依赖和mapstruct-processor注解处理器的依赖,使用Maven引入依赖一共有两种方式:

  • 以插件的形式添加mapstruct-processor

普通使用可参考 Mapstruct官方指导

<properties>
    <org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version> <!-- or newer version -->
            <configuration>
                <source>1.8</source> <!-- depending on your project -->
                <target>1.8</target> <!-- depending on your project -->
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                    <!-- other annotation processors -->
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

需要注意的是,项目中如果使用lombok,需要在编译插件中也配置lombok。否者可能会导致无法正常生成@Mapper接口的实现类。

             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>

 在springboot中使用此方式配置mapstruct,在IDEA会导致项目中自定义的Properties类发生提示Spring Boot COnfiguration Annotation Processor not configured,即使在项目中引入了依赖,在Properties上使用了@ConfigurationProperties注解依然会报错。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

并且在yml文件中无法进行属性提示,如图1

图1

                                                                                   

  •  使用纯依赖的方式进行配置

 在pom中进行如下配置,使用该配置在yml文件中进行自定义Properties类属性配置时,可以正常使用进行代码提示(图2)

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>


-----
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>${maven-resources-plugin.version}</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <!--<annotationProcessorPaths>-->
                    <!--    <path>-->
                    <!--        <groupId>org.mapstruct</groupId>-->
                    <!--        <artifactId>mapstruct-processor</artifactId>-->
                    <!--        <version>${mapstruct.version}</version>-->
                    <!--    </path>-->
                    <!--    <path>-->
                    <!--        <groupId>org.projectlombok</groupId>-->
                    <!--        <artifactId>lombok</artifactId>-->
                    <!--        <version>${lombok.version}</version>-->
                    <!--    </path>-->
                    <!--</annotationProcessorPaths>-->
                </configuration>
            </plugin>

 

图2

二、使用Mapstruct效果

import com.carlos.core.shiro.vo.LoginSysUserVo;
import com.carlos.system.pojo.entity.SysUser;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper
public interface SysUserConvert {

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

    LoginSysUserVo sysUserToLoginSysUserVo(SysUser sysUser);

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值