ripper234
ripper234

Reputation: 230366

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

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?

Upvotes: 23

Views: 57215

Answers (7)

Maayan Hope
Maayan Hope

Reputation: 1611

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

Upvotes: 0

inter18099
inter18099

Reputation: 87

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.

Upvotes: 3

dolbysurnd
dolbysurnd

Reputation: 736

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

Upvotes: 25

Green Lei
Green Lei

Reputation: 3422

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();

Upvotes: 13

Daniel de Zwaan
Daniel de Zwaan

Reputation: 3105

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

Upvotes: 1

bobbyberg
bobbyberg

Reputation: 65

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.

Upvotes: 1

ripper234
ripper234

Reputation: 230366

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.

Upvotes: 7

Related Questions