使用mapstruct时,return属性为null

时间:2020-03-15 03:37:47

标签: spring-boot mapstruct

嗨,我是Spring Boot 2和Java 11的Mapstruct新手。 我遵循了文档中的配置和用法,并解决了问题。

    @Autowired
    private EmployeeTargetMapper mapper;

    public ColumnName findById(Integer id) {
        Employee employee = employeeRepository.findById(id).get();
        OnlyNameDTO onlyName = mapper.fromEmployee(employee);
        return onlyName; // onlyName.firstName = null
    }

所以我再次阅读了文档。它会生成生成诸如queryDSL QClass之类的impl代码。
但是当我进行Maven安装时,也会出现错误。如果它通过Maven安装,肯定是Impl代码出来的吗?

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project tutorial-jpql: Fatal error compiling

我所有的代码都在这里

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.joons</groupId>
    <artifactId>tutorial-jpql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tutorial-jpql</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.querydsl/querydsl-jpa -->
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>4.2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.querydsl/querydsl-apt -->
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>4.2.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>6.3.0</version>
                <configuration>
                    <url>jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&amp;characterEncoding=UTF-8</url>
                    <user>root</user>
                    <password>password</password>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version> <!-- or newer version -->
                <configuration>
                    <source>1.11</source> <!-- depending on your project -->
                    <target>1.11</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>

</project>

映射器

@Mapper(componentModel = "spring")
public interface EmployeeTargetMapper {

    OnlyNameDTO fromEmployee(Employee employee);
}

1 个答案:

答案 0 :(得分:0)

好像您有2个插件,它们分别在编译时生成源代码(apt-maven-plugin)和(maven-compiler-plugin)。

我建议您仅使用maven-compiler-plugin

您需要将queryDSL处理器依赖性添加到annnotationProcessorPaths中(与MapStruct相同)。

根据您拥有的pom,我认为您应该在annotationProcessorPaths中添加以下内容:

<path>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <version>4.2.2</version>
</path>
<path>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
    <version>4.2.2</version>
</path>

并完全删除maven-apt-plugin