解决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的设置与文件实际的位置不同


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

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


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



上一篇:利用LVS-NAT和DR模型分别负载均衡一个php应用


下一篇:Lombok使用