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

在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节点

 
    <mappers>
        <mapper resource="com/smbms/dao/user/UserMapper.xml"/>
    mappers>

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

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

MyBatis报错:org.apache.ibatis.binding.BindingException: Type interface com.smbms.dao.provider.Provider_第1张图片将上面的namespace修改为正确位置后问题就解决了,如果不是这里出错那么久可能是xml中的mappers节点配置错误啦。

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

你可能感兴趣的:(MyBatis报错:org.apache.ibatis.binding.BindingException: Type interface com.smbms.dao.provider.Provider)