我的映射器接口存在问题,它在编译时创建异常:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建文件中定义的名为“bookServiceImpl”的 bean 时出错/home/emmanuel/Documents/Dev/spring app/hymnbook/hymnbook/target/classes/com/zanah/hymnbook/service/BookServiceImpl.class]:通过构造函数参数1表达的不满足的依赖关系:没有类型为“com.zanah”的合格bean .hymnbook.mapper.BookMapper' 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注释:{}
com.zanah.hymnbook.service.BookServiceImpl 中构造函数的参数 1 需要类型为“com.zanah.hymnbook.mapper.BookMapper”的 bean,但无法找到。
Mapper Interface
@Mapper(componentModel = "spring")
public interface BookMapper {
Book bookDtoToBook(BookDto bookDto);
BookDto bookToBookDto(Book book);
List<Book> BookDtoToBook(List<BookDto> bookDtoList);
List<BookDto> bookToBookDto(List<Book> bookList);
BookDto bookRequestDtoToBookDto(BookRequestDto bookRequestDto);
}
Service Implemenation class
@Service
@AllArgsConstructor
public class BookServiceImpl implements BookService{
@Autowired
private final BookRepository bookRepository;
@Autowired
private final BookMapper bookMapper;
@Override
public BookDto saveBook(BookDto bookDto) {
Book recordFromDB = bookRepository.findByName(bookDto.getName());
if (recordFromDB != null) {
throw new ResourceAlreadyExistException("Book", "ID", bookDto.getName());
}
Book recordToBeSaved = bookMapper.bookDtoToBook(bookDto);
Book savedRecord = bookRepository.save(recordToBeSaved);
BookDto result = bookMapper.bookToBookDto(savedRecord);
return result;
}
@Override
public List<BookDto> findAllBooks() {
return null;
}
@Override
public BookDto getBookById(Long id) {
return null;
}
@Override
public BookDto updateBook(BookDto Bookdto, Long id) {
return null;
}
@Override
public void deleteBook(Long id) {
}
}
Main Application
@SpringBootApplication
@ComponentScan(basePackages = "com.zanah.hymnbook")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我已经验证了 pom.xml 文件中的依赖关系以包含 mapstruct
这帮助我解决了我的问题https://stackoverflow.com/a/50204696/22585216我必须更新到更高版本才能使其正常工作3.11.0