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

2023-07-01 15:44:36 浏览数 (2)

环境说明: 系统:win10 专业版 开发环境:IDEA JDK版本:1.8 mysql:5.5 mybatis:3.5.3 Junit:5.7.0

问题再现:

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

代码语言:javascript复制
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

错误原因:

报错信息中出现了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的核心配置文件中添加如下内容:

代码语言:javascript复制

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

0 人点赞