Java 使用 mybatis 的“MapperRegistry 不知道类型接口”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4263832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:09:00  来源:igfitidea点击:

"Type interface is not known to the MapperRegistry" exception using mybatis

javaibatismybatis

提问by ripper234

I'm setting up mybatis using annotations, and getting this helpful exception

我正在使用注释设置 mybatis,并得到这个有用的异常

org.apache.ibatis.binding.BindingException: Type interface org.foo.Bar is not known to the MapperRegistry

org.apache.ibatis.binding.BindingException:MapperRegistry 不知道类型接口 org.foo.Bar

Googling itdoesn't find anything, nor the user guide. What am I missing?

谷歌搜索没有找到任何东西,也没有用户指南。我错过了什么?

采纳答案by ripper234

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.

好的,明白了 - 发生这种情况是因为我使用 XML 文件进行配置,并为映射器本身使用注释 - 而 mybatis 在使用 XML 配置时找不到映射器注释。

See this followup question.

请参阅此后续问题

回答by dolbysurnd

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

只适合那些因为对 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

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

回答by bobbyberg

It may be that your mapper.xml file is using the incorrect namespace (perhaps because of a copy paste error).

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

For instance, let's say you have a Java interface called MyEntityMapper.javathat shouldbe linked to a mybatis mapper xml config called MyEntityMapper.xml:

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

MyEntityMapper.java

MyEntityMapper.java

package my.mappers;

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

MyEntityMapper.xml

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 namespaceattribute on the <mapper>element in MyEntityMapper.xmlis pointing to some non-existent mapper non.existent.package.NonExistentMapper, when really it should be pointing to my.mappers.MyEntityMapper.

请注意,in 元素namespace上的属性指向某个不存在的 mapper ,而实际上它应该指向。<mapper>MyEntityMapper.xmlnon.existent.package.NonExistentMappermy.mappers.MyEntityMapper

回答by Daniel de Zwaan

Type interface org.domain.classmapperis not known to the MapperRegistry

MapperRegistry不知道类型接口org.domain.classmapper

MyBatis throws this exception if the full package / class is not entered into the mapper xml namespace.

如果完整的包/类没有输入到映射器 xml 命名空间中,MyBatis 会抛出这个异常。

e.g. <mapper namespace="classmapper">causes exception, but

例如 <mapper namespace="classmapper">导致异常,但是

<mapper namespace="org.domain.classmapper">works

<mapper namespace="org.domain.classmapper">作品

回答by Green Lei

add Mapperclass to your SqlSessionFactory Configuration as this:

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

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

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

SqlSession sqlSession = factory.openSession();

回答by inter18099

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

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

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.

你的映射器接口应该在 com.mapper 包中,它的名字是 LineMapper。

回答by Maayan Hope

Happened to me when creating a shadowJar/bootJar for a spring boot project and using org.springframework.boot gradle plugin

在为 spring boot 项目创建 shadowJar/bootJar 并使用 org.springframework.boot gradle 插件时发生在我身上

When the jars are zipped inside the bootJar myBatis may fail to find the XML configuration files and will throw the described exception

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

adding this block in the build.gradle file:

在 build.gradle 文件中添加这个块:

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

solved my problem

解决了我的问题