MyBatis 在 Java 项目启动时报错,原因非常多,常见的主要集中在 配置、依赖、XML、Mapper 扫描 这几类。下面我按「最常见 → 较少见」给你一个排查清单,你可以对照看看。
典型报错:
Invalid bound statement (not found)
✅ 检查点:
namespace 是否等于 Mapper 接口全路径<mapper namespace="com.xxx.mapper.UserMapper">
id 是否和接口方法名一致<select id="selectById" ...>
resources 对应目录中(包结构一致)典型报错:
No qualifying bean of type 'xxxMapper' available
✅ 检查:
@MapperScan("com.xxx.mapper")
或 Mapper 接口上加:
@Mapper
典型报错:
ClassNotFoundException
NoClassDefFoundError
✅ 检查依赖(Spring Boot):
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
典型报错:
Failed to configure a DataSource
✅ 检查:
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.xxx.entity
常见错误:
mapper-locations 写错典型报错:
Communications link failure
Access denied for user
✅ 检查:
url: jdbc:mysql://localhost:3306/db?serverTimezone=Asia/Shanghai
@Primary典型报错:
Error creating document instance
✅ 检查:
< 没转义<if test="age < 18">
应写成:
<if test="age < 18">
mybatis:
configuration:
map-underscore-to-camel-case: true
如果你愿意,把下面信息发我,我可以直接帮你定位:
✅ 总结一句话:
MyBatis 启动报错,99% 是 Mapper 没扫到、XML 不对、配置写错。
你可以把报错贴出来,我帮你一行一行看。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。