使用 mybatis 出现“MapperRegistry 不知道类型接口”异常
题
我正在使用注释设置 mybatis,并得到这个有用的异常
org.apache.ibatis.binding.BindingException:键入org.foo.bar bar不知道bar不知道
谷歌搜索它 没有找到任何东西,也没有找到用户指南。我缺少什么?
解决方案 3
好的,明白了 - 发生这种情况是因为我使用 XML 文件进行配置,并使用映射器本身的注释 - 而 mybatis 在使用 XML 配置时找不到映射器注释。
看到这个 后续问题.
其他提示
只适合那些因为 mybatis 新手而来到这里的人http://www.mybatis.org/core/configuration.html
http://www.mybatis.org/mybatis-3/configuration.html
在配置文件映射器部分
<mappers>
<mapper class="my.package.com.MyClass"/>
</mappers>
这将使您启动并运行 config.xml 和带注释的接口
添加 映射器 类到您的 SqlSessionFactory 配置如下:
SqlSessionFactory factory = new SqlSessionFactoryBuilder()
.build(reader);
//very import
factory.getConfiguration().addMapper(BarMapper.class);
SqlSession sqlSession = factory.openSession();
在您的mapper.xml 文件中,映射器的命名空间应该是映射器接口的路径。
例如:
<mapper namespace="com.mapper.LineMapper">
<select id="selectLine" resultType="com.jiaotong114.jiaotong.beans.Line">
select * from bus_line where id = #{id}
</select>
</mapper>
你的映射器接口应该在 com.mapper 包中,它的名称是 LineMapper。
您的 mapper.xml 文件可能使用了不正确的命名空间(可能是由于复制粘贴错误)。
例如,假设您有一个名为的 Java 接口 MyEntityMapper.java
那 应该 链接到名为的 mybatis 映射器 xml 配置 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>
请注意, namespace
属性上的 <mapper>
元素在 MyEntityMapper.xml
指向一些不存在的映射器 non.existent.package.NonExistentMapper
, ,实际上它应该指向 my.mappers.MyEntityMapper
.
类型接口 org.domain.classmapper MapperRegistry 不知道
如果完整的包/类没有输入到映射器 xml 命名空间中,MyBatis 会抛出此异常。
例如<mapper namespace="classmapper">
导致异常,但是
<mapper namespace="org.domain.classmapper">
作品
在为春季启动项目和org.springframework.boot gradle插件创建Shadowjar/bootjar时发生的事情发生在我身上。
当 jar 被压缩到 bootJar 中时,myBatis 可能无法找到 XML 配置文件,并会抛出所描述的异常
在 build.gradle 文件中添加此块:
bootJar {
requiresUnpack '**/MyProblematic.jar'
}
解决了我的问题