0

我正在使用 Spring,我从项目中的 mapstruct 库开始,所以我想为 @Mapper(componentModel = "spring") 创建原型注释,如 @SpringMapper。但它不会生成任何映射器。

这是不可能的还是我做错了什么?

4

2 回答 2

1

That's a very interesting idea, but it's not supported currently. The MapStruct annotation processor only looks for @Mapper annotations, i.e. it will be invoked for the definition of @SpringMapper, but not for any beans annotated with @SpringMapper.

That being said, MapStruct provides its own means of reusing configuration via config classes:

@MapperConfig(componentModel="spring")
public interface SpringMappers {}

Put all common options like the component model to such config class and then reference it from individual mappers:

@Mapper(config=SpringMappers.class)
public interface MyMapper {
    //...
}
于 2017-03-12T17:56:46.813 回答
0

感谢您的回答Gunnar

maven 的另一种可能的解决方案是:以下 maven 插件将设置默认(项目范围)componentModel=spring 和 unmappedTargetPolicy=ERROR。

 <properties>
        <java.version>1.8</java.version>
        <org.mapstruct.version>1.1.0.Final</org.mapstruct.version>
    </properties>


    <dependencies>
         <dependency>
              groupId>org.mapstruct</groupId>
              <artifactId>mapstruct-jdk8</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>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>UTF-8</encoding>
                        <showDeprecation>true</showDeprecation>
                        <showWarnings>true</showWarnings>
                        <debug>true</debug>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.bsc.maven</groupId>
                    <artifactId>maven-processor-plugin</artifactId>
                    <version>3.2.0</version>
                    <configuration>
                        <defaultOutputDirectory>
                            ${project.build.directory}/generated-sources
                        </defaultOutputDirectory>
                        <processors>
                            <processor>org.mapstruct.ap.MappingProcessor</processor>
                        </processors>
                        <options>
                            <mapstruct.defaultComponentModel>spring</mapstruct.defaultComponentModel>
                            <mapstruct.unmappedTargetPolicy>ERROR</mapstruct.unmappedTargetPolicy>
                        </options>
                    </configuration>
                    <executions>
                        <execution>
                            <id>process</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>process</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
于 2017-03-24T19:46:25.707 回答