SpringBoot 无法自动装配。未找到 'UserMapper' 类型的 beans 的 Mapstruct 映射器错误

SpringBoot Could not autowire. No beans of 'UserMapper' type found error for Mapstruct Mappers

这是我的用户 class。

@Entity
@Table(name = "user_table")
@Data
public class User {

@Id
private Long id;
private String userName;
private String password;
 }

这是我的 UserDto,我知道字段名称相同。但我正在尝试理解这个概念。

@Data
public class UserDto {

private Long id;
private String userName;
private String password;
}

我想使用 MapStruct 进行到 Entity-Dto 和 Dto-Entity 的转换。所以我写了一个像 EntityMapper 这样的基础接口。

public interface EntityMapper<D, E> {

E toEntity(D dto);

D toDto(E entity);

List<E> toEntity(List<D> dtoList);

List<D> toDto(List<E> entityList);

}

然后我创建了一个扩展了 EntityMapper 的新接口 .

@Mapper(componentModel = "spring", uses = UserService.class)

public interface UserMapper extends EntityMapper<UserDto , User> {


@Mapping(source = "id", target = "id")
@Mapping(source = "userName", target = "userName")
@Mapping(source = "password", target = "password")
@Override
User toEntity(UserDto dto);

@Override
UserDto toDto(User entity);

@Override
List<User> toEntity(List<UserDto> dtoList);

@Override
List<UserDto> toDto(List<User> entityList);

这是我的控制器

@RestController
@RequestMapping("/v1/api/users")
public class UserController {

private final UserService userService;

public UserController(UserService userService) {
    this.userService = userService;
}

@PostMapping("/save")
public ResponseEntity<UserDto> saveUser(@RequestBody UserDto userDto){

    UserDto res = userService.saveUser(userDto);
    return ResponseEntity.ok(res);
}
}

最后,当我尝试创建新服务时 class 并使用来自 UserMapper 接口的引用。 spring告诉我

无法自动装配。找不到 'UserMapper' 类型的 bean。

对于这一行

   private final UserMapper userMapper;

这就是我的全部服务 Class。当我删除 @Service 注释时,错误正在修复。但这是一项服务 class 所以我认为我必须添加 @Service 注释。

我尝试将@Component 注释添加到UserMapper。但是应用程序启动失败。

@Service
public class UserService {

private final UserRepository userRepository;
private final UserMapper userMapper;

public UserService(UserRepository userRepository, UserMapper userMapper) {
    this.userRepository = userRepository;
    this.userMapper = userMapper;
}

public UserDto saveUser(UserDto userDto){

    User user = userMapper.toEntity(userDto);
    user = userRepository.save(user);
    UserDto res = userMapper.toDto(user);
    return res;
}

}

这里是 pom.xml.

  <?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.6.6</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>AprilSevenApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>AprilSevenApp</name>
<description>AprilSevenApp</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.4.2.Final</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.dot</groupId>
        <artifactId>util</artifactId>
        <version>0.2.0</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

我刚刚为 mapstruct 添加了这个依赖项。如果我必须添加不同的依赖项,我不知道。

<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -- 
 >
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.4.2.Final</version>
</dependency>

注释处理器必须添加到编译器插件才能为 UserMapper 接口生成实现。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <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>

另外我认为不能删除 uses = UserService.class,因为 UserService 不是 Mapper。这里是uses注解参数的官方文档:

Other mapper types used by this mapper. May be hand-written classes or other mappers generated by MapStruct. No cycle between generated mapper classes must be created.