Mybatis Type interface xxxMapper is not known to the MapperRegistry

在这里插入图片描述

1. 问题

在执行 MyBatis 快速入门项目时抛出如下异常:

Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com.mybatis.example.mapper.UserMapper is not known to the MapperRegistry.
	at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
	at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:876)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:288)
	at com.mybatis.example.MybatisQuickStartMapper.main(MybatisQuickStartMapper.java:32)

2. 解决方案

上述异常信息的出现一般是因为 mybatis-config.xml 的 mapper 的配置出错了,无法找到相应的 UserMapper 接口的 UserMapper.xml 的映射文件:

<mappers>
     <mapper resource="UserMapper.xml"/>
</mappers>

通过 mybatis-config.xml 核心配置文件可以知道需要在 resource 根目录下有一个 UserMapper.xml 配置文件,检查发现没有问题。查看 UserMapper.xml 配置文件,命名空间为 com.mybatis.example.UserMapper

<?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">

<!-- namespace 命名空间 -->
<mapper namespace="com.mybatis.example.UserMapper">
    <!-- 查询所有的用户 -->
    <select id="selectAll" resultType="com.mybatis.example.pojo.User">
        select * from tb_user;
    </select>

    <!-- 根据指定的 Id 查询用户 -->
    <select id="selectById" resultType="com.mybatis.example.pojo.User">
        select * from tb_user where id = #{id};
    </select>
</mapper>

发现命名空间与我们 UserMapper 接口的包路径 com.mybatis.example.mapper.UserMapper 不一致,所以才导致上述异常的出现。如果要使用 Mapper 代理开发必须满足 设置 SQL 映射文件的 namespace 属性为 Mapper 接口全限定名 的要求:

在这里插入图片描述

详细请查阅Mybatis 快速入门

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值