项目场景
最近,在跟着黑马IT的视频学习mybatis,现记录如下
开发环境描述
IDEA版本:20201.1
JDK版本:jdk1.8.0_181、jdk-14.0.2、jdk-16.0.1
Maven版本:apache-maven-3.5.2
该问题出现在以上版本,并成功解决。
问题描述
出现报错:Type interface com.itheima.dao.IUserDao is not known to the MapperRegistry.
解决方案
核对 SqlMapConfig.xml
中信息径发现并无异常,最终发现自己的 namespace
写错了。
修改 namespace
再次运行,成功解决
原因分析
一般出现 Type interface com.itheima.dao.IUserDao is not known to the MapperRegistry.
异常时,我们应该安装以下步骤进行排查:
- 检查
SqlMapConfig.xml
中有没有配置mapper
路径 - 检查
SqlMapConfig.xml
中resource
指定位置是都有误 - 检查路径中的
namespace
是否配置错误
其他可能出现的错误
需要注意,如果报错是org.apache.ibatis.exceptions.PersistenceException:### Error querying database.
需要根据后面的 Cause:xxx
来判断错误类型
resultType
配置错误的话,会报 Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating interface com.itheima.dao.IUserDao with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.itheima.dao.IUserDao.<init>()
异常
sql语句错误的话,会报 Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'eesy_mybatis.usr' doesn't exist
异常
参考文献
1.Mybatis:Type interface com.itheima.dao.IUserDao is not known to the MapperRegistry.解决方案
写在后面
欢迎随时留言讨论,知无不答!