您的位置:首页 > 运维架构 > Apache

MyBatis报错:org.apache.ibatis.binding.BindingException: Type interface com.smbms.dao.provider.Provider

2019-08-15 19:35 1391 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_43199016/article/details/99652033

在Java使用MyBatis框架开发时,遇到报错:

org.apache.ibatis.binding.BindingException: Type interface com.smbms.dao.provider.ProviderMapper is not known to the MapperRegistry.
的解决方案。

首先我们分析这个错误,这个错误报的是没有绑定映射文件

binding.BindingException
,所以这个错误只要在MyBatis中加上对应的配置文件即可。

org.apache.ibatis.binding.BindingException: Type interface com.smbms.dao.provider.ProviderMapper is not known to the MapperRegistry.
at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:779)
at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:291)
at com.smbms.test.TestProviderMapper.testGetProviderCount(TestProviderMapper.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.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:389)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:1

解决方案(在mybatis-config.xml中加入映射文件):

未加入所需配置文件时的mybatis-config.xml中的mappers节点

<!--配置SQL映射文件-->
<mappers>
<mapper resource="com/smbms/dao/user/UserMapper.xml"/>
</mappers>

加入所需配置文件时的mybatis-config.xml中的mappers节点

<!--配置SQL映射文件-->
<mappers>
<mapper resource="com/smbms/dao/user/UserMapper.xml"/>
<!--添加为映射的文件-->
<mapper resource="com/smbms/dao/user/ProviderMapper.xml"/>
</mappers>

出现这种错误的第二种类型:

org.apache.ibatis.binding.BindingException: Type interface com.hx.dao.provider.ProviderMapper is not known to the MapperRegistry.
at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:42)
at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:639)
at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:218)
at com.hx.dao.provider.ProviderMapperTest.testGetProviderList(ProviderMapperTest.java:46)
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)

注意报错中提示的:

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

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

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

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐