“MapperRegistry 不知道类型接口”使用mybatis时出现异常
我正在使用注释设置 mybatis,并得到这个有用的异常
org.apache.ibatis.binding.BindingException: 类型接口 org.foo.Bar 不是 MapperRegistry已知
谷歌搜索没有找到任何东西,也没有找到用户指南。我缺少什么?
I'm setting up mybatis using annotations, and getting this helpful exception
org.apache.ibatis.binding.BindingException:
Type interface org.foo.Bar is not
known to the MapperRegistry
Googling it doesn't find anything, nor the user guide. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只适合那些因为 mybatis 新手而来到这里的人
http://www.mybatis.org/core/configuration.html< /罢工>http://www.mybatis.org/mybatis-3/configuration.html
在配置文件映射器部分中,
您将使用 config.xml 和带注释的接口启动并运行
just for anyone who ended up here because they're new to mybatis
http://www.mybatis.org/core/configuration.htmlhttp://www.mybatis.org/mybatis-3/configuration.html
in the config file mappers section
this will have you up and running with a config.xml and annotated interfaces
将 Mapper 类添加到您的 SqlSessionFactory 配置中,如下所示:
add Mapper class to your SqlSessionFactory Configuration as this:
好的,明白了 - 发生这种情况是因为我使用 XML 文件进行配置,并使用映射器本身的注释 - 而 mybatis 在使用 XML 配置时找不到映射器注释。
请参阅此后续问题。
OK, got it - this is happening because I was using a XML file for the configuration, and annotations for the mappers themselves - and mybatis doesn't find mapper annotations when using an XML config.
See this followup question.
在您的mapper.xml 文件中,映射器的命名空间应该是映射器接口的路径。
例如:
您的映射器接口应位于 com.mapper 包中,其名称为 LineMapper。
In your mapper.xml file mapper's namespace should be the path to the mapper interface.
for example:
your mapper interface should be in com.mapper package and the name of it is LineMapper.
您的 mapper.xml 文件可能使用了不正确的命名空间(可能是由于复制粘贴错误)。
例如,假设您有一个名为
MyEntityMapper.java
的 Java 接口,它应该链接到名为MyEntityMapper.xml
的 mybatis 映射器 xml 配置:MyEntityMapper.java
MyEntityMapper.xml
请注意,
元素上的namespace
属性MyEntityMapper.xml 指向某个不存在的映射器non.existent.package.NonExistentMapper
,而实际上它应该指向my.mappers.MyEntityMapper
。It may be that your mapper.xml file is using the incorrect namespace (perhaps because of a copy paste error).
For instance, let's say you have a Java interface called
MyEntityMapper.java
that should be linked to a mybatis mapper xml config calledMyEntityMapper.xml
:MyEntityMapper.java
MyEntityMapper.xml
Notice that the
namespace
attribute on the<mapper>
element inMyEntityMapper.xml
is pointing to some non-existent mappernon.existent.package.NonExistentMapper
, when really it should be pointing tomy.mappers.MyEntityMapper
.MapperRegistry 不知道类型接口org.domain.classmapper
如果完整的包/类没有输入到映射器 xml 命名空间中,MyBatis 会抛出此异常。
例如
导致异常,但
有效Type interface org.domain.classmapper is not known to the MapperRegistry
MyBatis throws this exception if the full package / class is not entered into the mapper xml namespace.
e.g.
<mapper namespace="classmapper">
causes exception, but<mapper namespace="org.domain.classmapper">
works为 Spring Boot 项目创建 ShadowJar/bootJar 时发生过这种情况
使用 org.springframework.boot gradle 插件
当 jar 被压缩到 bootJar 内时,myBatis 可能无法找到 XML 配置文件,并会抛出所描述的异常,
在 build.gradle 文件中添加此块:
解决了我的问题
Happened to me when creating a shadowJar/bootJar for a spring boot project and
using org.springframework.boot gradle plugin
When the jars are zipped inside the bootJar myBatis may fail to find the XML configuration files and will throw the described exception
adding this block in the build.gradle file:
solved my problem