找不到按名称限定的 Mapstruct

Mapstruct Qualified By Name Not Found

我有一个 Post 实体和一个 Post DTO,目前正在尝试将 DTO 映射到实体,但它给了我一个 Qualifier error. No method found annotated with @Named#value: [ getUserByUsername ]. 任何帮助将不胜感激,这些是我当前的文件:

PostDTO:

@Getter
@Setter
public class PostDTO {

    private String author;

    @Size(max = 755)
    private String content;

}

Post实体:

@Entity
@Table(name = "POST")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Post {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @ManyToOne(fetch = FetchType.EAGER, cascade= CascadeType.ALL)
    @JoinColumn(name = "POSTS")
    private User author;

    @Column(name = "POSTED_AT", nullable = false)
    private LocalDateTime dateTime;

    @Size(max = 755)
    @Column(name = "CONTENT", nullable = false, length = 755)
    private String content;

}

Post映射器:

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, uses = {MappingUtil.class, PostRepository.class})
public interface PostMapper {

    // since the author attribute on postDTO is a String, we use mapstruct to find
    // within our database the matching user to that username and return the user object
    @Mapping(source = "author", target = "author", qualifiedByName = {"getUserByUsername"})
    Post toEntity(PostDTO postDTO);

    PostDTO toDTO(Post post);

}

MappingUtils:

@Component
public class MappingUtil {

    private final UserRepository userRepository;

    MappingUtil(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @BeanMapping(qualifiedByName = "getUserByUsername")
    public User getUserByUsername(String username){
        return userRepository.findByUsername(username);
    }

}

因此,每当我 运行 构建时,映射器都无法在 MappingUtil class 上使用 getUserByUsername 方法找到 @Named 注释 class,我尝试了一些方法,但是似乎无法修复它

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, uses = {MappingUtil.class, PostRepository.class})
public interface PostMapper {

    // since the author attribute on postDTO is a String, we use mapstruct to find
    // within our database the matching user to that username and return the user object
    @Mapping(source = "author", target = "author", qualifiedByName = "getUserByUsername")
    Post toEntity(PostDTO postDTO);

    PostDTO toDTO(Post post);

}

@Component
public class MappingUtil {

    private final UserRepository userRepository;

    MappingUtil(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Named("getUserByUsername")
    public User getUserByUsername(String username){
        return userRepository.findByUsername(username);
    }

}

您能否尝试在您的方法上使用@Named 注释。这将有助于 map-struct 找到 运行 映射您的字段所需的方法。

封装私有构造函数? 尝试将 public 添加到构造函数中:

public MappingUtil(UserRepository userRepository) {
        this.userRepository = userRepository;
}

注解@BeanMapping 对你没有帮助。使用@Name 代替,错误将消失。

@Named("getUserByUsername")
public User getUserByUsername(String username) {
    return userRepository.findByUsername(username);
}

您还需要在 toDTO(Post post) 中将 User 转换为 UserName 的映射,如下所示:

@Mapping(source = "author.userName", target = "author")
PostDTO toDTO(Post post);