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