“MapperRegistry 不知道类型接口”使用mybatis时出现异常

发布于 2024-10-04 06:57:16 字数 394 浏览 4 评论 0原文

我正在使用注释设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

辞旧 2024-10-11 06:57:16

只适合那些因为 mybatis 新手而来到这里的人
http://www.mybatis.org/core/configuration.html< /罢工>
http://www.mybatis.org/mybatis-3/configuration.html

在配置文件映射器部分中,

<mappers>
<mapper class="my.package.com.MyClass"/>
</mappers>

您将使用 config.xml 和带注释的接口启动并运行

just for anyone who ended up here because they're new to mybatis
http://www.mybatis.org/core/configuration.html
http://www.mybatis.org/mybatis-3/configuration.html

in the config file mappers section

<mappers>
<mapper class="my.package.com.MyClass"/>
</mappers>

this will have you up and running with a config.xml and annotated interfaces

那一片橙海, 2024-10-11 06:57:16

Mapper 类添加到您的 SqlSessionFactory 配置中,如下所示:

SqlSessionFactory factory = new SqlSessionFactoryBuilder()
            .build(reader);

//very import
factory.getConfiguration().addMapper(BarMapper.class);

SqlSession sqlSession = factory.openSession();

add Mapper class to your SqlSessionFactory Configuration as this:

SqlSessionFactory factory = new SqlSessionFactoryBuilder()
            .build(reader);

//very import
factory.getConfiguration().addMapper(BarMapper.class);

SqlSession sqlSession = factory.openSession();
玩套路吗 2024-10-11 06:57:16

好的,明白了 - 发生这种情况是因为我使用 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.

—━☆沉默づ 2024-10-11 06:57:16

在您的mapper.xml 文件中,映射器的命名空间应该是映射器接口的路径。

例如:

<mapper namespace="com.mapper.LineMapper">
<select id="selectLine" resultType="com.jiaotong114.jiaotong.beans.Line">
select * from bus_line where id = #{id}
</select>
</mapper>

您的映射器接口应位于 com.mapper 包中,其名称为 LineMapper。

In your mapper.xml file mapper's namespace should be the path to the mapper interface.

for example:

<mapper namespace="com.mapper.LineMapper">
<select id="selectLine" resultType="com.jiaotong114.jiaotong.beans.Line">
select * from bus_line where id = #{id}
</select>
</mapper>

your mapper interface should be in com.mapper package and the name of it is LineMapper.

情释 2024-10-11 06:57:16

您的 mapper.xml 文件可能使用了不正确的命名空间(可能是由于复制粘贴错误)。

例如,假设您有一个名为 MyEntityMapper.java 的 Java 接口,它应该链接到名为 MyEntityMapper.xml 的 mybatis 映射器 xml 配置:

MyEntityMapper.java

package my.mappers;

public interface MyEntityMapper {
    MyEntity getById(@Param("id") String id);
}

MyEntityMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="non.existent.package.NonExistentMapper">

    <resultMap id="MyEntityResultmap" type="MyEntity">
        <!-- some result map stuff here -->
    </resultMap>

    <select id="getByUuid" resultMap="MyEntityResultMap">
        <!-- some sql code here -->
    </select>
</mapper>

请注意, 元素上的 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 called MyEntityMapper.xml:

MyEntityMapper.java

package my.mappers;

public interface MyEntityMapper {
    MyEntity getById(@Param("id") String id);
}

MyEntityMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="non.existent.package.NonExistentMapper">

    <resultMap id="MyEntityResultmap" type="MyEntity">
        <!-- some result map stuff here -->
    </resultMap>

    <select id="getByUuid" resultMap="MyEntityResultMap">
        <!-- some sql code here -->
    </select>
</mapper>

Notice that the namespace attribute on the <mapper> element in MyEntityMapper.xml is pointing to some non-existent mapper non.existent.package.NonExistentMapper, when really it should be pointing to my.mappers.MyEntityMapper.

如果没有 2024-10-11 06:57:16

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

空‖城人不在 2024-10-11 06:57:16

为 Spring Boot 项目创建 ShadowJar/bootJar 时发生过这种情况
使用 org.springframework.boot gradle 插件

当 jar 被压缩到 bootJar 内时,myBatis 可能无法找到 XML 配置文件,并会抛出所描述的异常,

在 build.gradle 文件中添加此块:

bootJar {
    requiresUnpack '**/MyProblematic.jar'
}

解决了我的问题

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:

bootJar {
    requiresUnpack '**/MyProblematic.jar'
}

solved my problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文