解决Mybatis报错问题:Type interface com.tjcu.dao.UserDao is not known to the MapperRegistry.

简介: 解决Mybatis报错问题:Type interface com.tjcu.dao.UserDao is not known to the MapperRegistry.

解决Mybatis报错问题:Type interface com.tjcu.dao.UserDao is not known to the MapperRegistry.

一、异常

org.apache.ibatis.binding.BindingException: Type interface com.tjcu.dao.UserDao is not known to the MapperRegistry.
  at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:36)
  at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:536)
  at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:215)
  at com.tjcu.util.MybatisUtil.getMapper(MybatisUtil.java:57)
  at com.tjcu.test.UserTest.testSelectAll(UserTest.java:22)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
  at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
  at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
  at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
  at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:221)
  at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

二、解决方案分为两种情况:

1.在mybatis核心配置文件中(mybatis-config.xml)配置mappers

<!--  每个mappers.xml文件都需要在mybatis-config.xml核心配置文件中注册-->
<mappers>
  <mapper resource="com/tjcu/dao/UserDaoImpl.xml"></mapper>
</mappers>

2.就是namespace编写出错(就是namespace的位置与实际文件位置不同)

注意报错中提示的:is not known to the MapperRegistry.这个表示没有这个映射地址,也就是说虽然配置文件中配置了,又能是SQL配置文件Mapper.xml中配置错误了,也有可能namespace中编写时出了错误,看下面一个实例:


namespace的设置与文件实际的位置不同


image.png

将上面的namespace修改为正确位置后问题就解决了,如果不是这里出错那么就可能是xml中的mappers节点配置错误啦。


完成后这个问题就解决了,如果以上步骤完成后运行时还报错那么说明,那么xml配置文件错误了,查找方向为优先SQL映射文件(Mapper结尾的文件),然后再看mybatis-config.xml的错误,不过虽然MyBatis框架一般都是xml配置文件的编写错误,还是需要注意自己的Java代码是否有写正确!



相关文章
|
6天前
|
Java 数据库连接 mybatis
mybatis参数报错Parameter ‘docId‘ not found. Available parameters are [arg1, arg0, param1, param2]
mybatis参数报错Parameter ‘docId‘ not found. Available parameters are [arg1, arg0, param1, param2]
|
2月前
|
SQL
解决一个mybatisplus left join里有ur报错问题
解决一个mybatisplus left join里有ur报错问题
20 0
|
2月前
|
SQL 安全 BI
基于jeecg-boot的nbcio-boot因升级mybatis-plus到3.5.3.1和JSQLParser 到4.6而引起的在线报表配置报错处理
基于jeecg-boot的nbcio-boot因升级mybatis-plus到3.5.3.1和JSQLParser 到4.6而引起的在线报表配置报错处理
55 0
|
2月前
|
Java 数据库连接 数据库
mybatis-plus报错:Can not find table primary key in Class
mybatis-plus报错:Can not find table primary key in Class
332 1
|
2月前
|
SQL XML Java
mybatis 调用修改SQL时 出现了一个问题 没有修改成功也没有报错
mybatis 调用修改SQL时 出现了一个问题 没有修改成功也没有报错
27 0
|
1天前
|
Java 数据库连接 数据库
Spring Boot与MyBatis的集成应用
Spring Boot与MyBatis的集成应用
|
5天前
|
Java 数据库连接 数据库
Spring Boot与MyBatis的集成应用
Spring Boot与MyBatis的集成应用
|
5天前
|
SQL Java 数据库连接
Spring Boot整合MyBatis
Spring Boot整合MyBatis
|
5天前
|
Java 数据库连接 Spring
Spring 整合 MyBatis 底层源码解析
Spring 整合 MyBatis 底层源码解析
|
6天前
|
Java 数据库连接 数据库
Spring Boot 集成 MyBatis-Plus 总结
Spring Boot 集成 MyBatis-Plus 总结