Osmanthus

空想具現化


  • 首页
  • 归档
  • 分类
  • 标签
  • 关于
  •   

© 2024 Homurax

UV: | PV:

Theme Typography by Makito

Proudly published with Hexo

MapStruct结合lombok使用

发布于 2020-01-15 Java  踩坑记录 

MapStruct 官方文档 中给出的maven-compiler-plugin插件的版本是3.5.1。

在 MapStruct 结合 lombok 使用时仍然会报错,不会编译出 Mapper 的实现类。

调整插件版本至3.6.1或以上即可解决这个问题。

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

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

两种常用的 Mapper 获取方式:

  1. 通过 mappers factory

    import org.mapstruct.Mapper;
    import org.mapstruct.factory.Mappers;
    
    @Mapper
    public interface YourMapper {
    
        YourMapper INSTANCE = Mappers.getMapper(YourMapper.class);
    }
    
  2. componentModel指定为spring,使用依赖注入(@Autowired)的方式获取

    import org.mapstruct.Mapper;
    
    @Mapper(componentModel = "spring")
    public interface YourMapper {
    
    }
    

使用参考文档 MapStruct 1.3.1.Final Reference Guide

 上一篇: Jetbrains Quest 1 记录 下一篇: Python脚本 discuz论坛登录 

© 2024 Homurax

UV: | PV:

Theme Typography by Makito

Proudly published with Hexo