“Type interface is not known to the MapperRegistry

2020-05-23 03:32发布

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?

7条回答
乱世女痞
2楼-- · 2020-05-23 03:40

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();
查看更多
We Are One
3楼-- · 2020-05-23 03:40

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.

查看更多
三岁会撩人
4楼-- · 2020-05-23 03:43

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.

查看更多
疯言疯语
5楼-- · 2020-05-23 03:53

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.

查看更多
Rolldiameter
6楼-- · 2020-05-23 03:53

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

查看更多
三岁会撩人
7楼-- · 2020-05-23 03:58

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

查看更多
登录 后发表回答