项目场景

最近,在跟着黑马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.

image-20220207162607830

解决方案

核对 SqlMapConfig.xml 中信息径发现并无异常,最终发现自己的 namespace 写错了。

image-20220207162743496

修改 namespace

image-20220207163918126

再次运行,成功解决

image-20220207163940371

原因分析

一般出现 Type interface com.itheima.dao.IUserDao is not known to the MapperRegistry. 异常时,我们应该安装以下步骤进行排查:

  1. 检查 SqlMapConfig.xml 中有没有配置 mapper 路径
  2. 检查 SqlMapConfig.xmlresource 指定位置是都有误
  3. 检查路径中的 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.解决方案

写在后面

欢迎随时留言讨论,知无不答!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐