报错日志
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2025-12-19 10:29:35.762 ERROR 580 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
*************************** APPLICATION FAILED TO START ***************************
Description: Field userMapper in nuc.edu.testspring.Service.Impl.UserServiceImpl required a bean of type 'nuc.edu.testspring.mapper.UserMapper' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action: Consider defining a bean of type 'nuc.edu.testspring.mapper.UserMapper' in your configuration.
进程已结束,退出代码为 1
解决方案
步骤 1:修复依赖兼容性问题(避免隐性报错)
你的mybatis-spring-boot-starter版本是 3.0.0,而 Spring Boot 2.6.13 和该版本不兼容(3.0.0 要求 Spring Boot 2.7+),需调整版本:
pom
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> <!-- 适配Spring Boot 2.6.x --> </dependency>步骤 2: 给 Mapper 接口添加@Mapper注解
在UserMapper接口上添加 MyBatis 的@Mapper注解,示例:
java
package nuc.edu.testspring.mapper; import nuc.edu.testspring.entity.User; import org.apache.ibatis.annotations.Mapper; // 关键注解 import java.util.List; @Mapper // 标记为MyBatis Mapper,让Spring扫描并创建Bean public interface UserMapper { // 你的Mapper方法(比如查询用户) User selectById(Long id); }