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

7

23

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?

Optic answered 24/11, 2010 at 5:48 Comment(1)
I love Google. 5 minutes after asking this, the Google link I posted points only to this question.Optic
O
7

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.

Optic answered 24/11, 2010 at 7:48 Comment(0)
A
25

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

Anatola answered 16/11, 2012 at 20:39 Comment(0)
L
13

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();
Lengthwise answered 12/7, 2015 at 7:40 Comment(0)
O
7

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.

Optic answered 24/11, 2010 at 7:48 Comment(0)
P
3

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.

Plasmagel answered 23/3, 2017 at 14:2 Comment(0)
N
1

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.

Neighbor answered 27/1, 2015 at 19:54 Comment(0)
C
1

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

Chevalier answered 24/4, 2015 at 6:53 Comment(0)
C
0

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

Coacher answered 1/5, 2019 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.