Cannot find related Attribute using Mapstruct Spring Boot

问题描述 投票:0回答:4

我正在按照以下文档来实现映射器接口: MapStruct- Baeldung for Employee 和 EmployeeDTO.

这是我的员工班级:

public class Employee {
private int id;
private String name;
// getters and setters
}

这是我的 EmployeeDTO 类:

public class EmployeeDTO {
private int employeeId;
private String employeeName;
// getters and setters
}

这是我的 Mapper 代码:

@Mapper
public interface EmployeeMapper {
@Mapping(target="employeeId", source="entity.id")
@Mapping(target="employeeName", source="entity.name")
EmployeeDTO employeeToEmployeeDTO(Employee entity);

@Mapping(target="id", source="dto.employeeId")
@Mapping(target="name", source="dto.employeeName")
Employee employeeDTOtoEmployee(EmployeeDTO dto);
}

我通过将鼠标悬停在 entity.id 上得到“找不到相关属性”。 entity.name, dto.employeeId 和 dto.employeeName.

为什么会这样?如果我删除实体和 dto 上的点操作并只写字段名称,它工作正常。

java spring spring-boot mapstruct mapper
4个回答
0
投票

我复制了同样的东西,它对我来说用点运算符工作得很好。也许尝试将 annotationProcessorPaths 部分添加到 baeldung document

中存在的 maven-compiler-plugin 插件的配置部分

文档中给出的这个插件:

<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.4.2.Final</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

试试看是否有帮助


0
投票

将@Entity 放在 Employee 和 EmployeeDTO 类之上


0
投票

不要忘记在 EmployeeDTO 类上使用@Data,在 Employee 类上使用@Entity


0
投票

通常,当有多个映射源时,应该使用

<parameter-name>.
来引用正确的参数。但是当映射方法只有一个源参数时,可以直接使用字段名。

IntelliJ 有一个插件,Mapstruct Support,它为使用 Mapstruct 的项目提供代码完成和其他功能。安装后,

Can't find related attribute
错误不再出现在我的案例中。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.