踩坑实录|mybatis项目报错:...Exception: Type interface UserMapper is not known to the MapperRegistry

踩坑实录|mybatis项目报错:...Exception: Type interface UserMapper is not known to the MapperRegistry

知识分子没文化
2021-05-25 / 0 评论 / 164 阅读 / 正在检测是否收录...

环境说明:

系统:win10 专业版

开发环境:IDEA

JDK版本:1.8

mysql:5.5

mybatis:3.5.3

Junit:5.7.0

问题再现:

mybatis项目运行时报错,报错信息如下:

org.apache.ibatis.binding.BindingException: Type interface com.langp.dao.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:779)
    at com.langp.dao.UserMapperTest.getUserList(UserMapperTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:498)
    ·····

Process finished with exit code -1

01

错误原因:

报错信息中出现了Type interface com.langp.dao.UserMapper is not known to the MapperRegistry,简单翻译一下就是:类型接口com.langp.dao.UserMapper不为MapperRegistry所知

所以这个错误肯定是与mapper有关系的。每一个Mapper.xml都需要在mybatis核心配置文件中进行注册,由于mybatis的核心配置文件mybatis-config.xml中缺少对应接口的Mapper.xml,所以运行时就会报错。

解决方法:

在mybatis的核心配置文件中添加如下内容:

<!-- 每一个mapper.xml配置文件都需要在mybaits核心文件中注册 -->
<mappers>
    <mapper resource="com/langp/dao/UserMapper.xml"></mapper>
</mappers>

其中,resource属性的值是接口对应Mapper.xml文件。添加之后即可解决。

0

评论 (0)

取消