springboot项目报错找不到mapper文件

发布时间 2023-07-23 14:25:20作者: 李蔚泽

在使用 SpringBoot 中的 MyBatis-Plus(简称 MP)时,出现无法找到 mapper.xml 文件的错误,可能有以下几个原因:
1.未正确配置 mapper.xml 文件路径:在 SpringBoot 中,可以通过在 application.properties 或 application.yml 文件中设置 mybatis-plus.mapper-locations 属性来指定 mapper.xml 文件的位置。如果该属性没有设置或设置错误,MyBatis-Plus 将无法找到 mapper.xml 文件,导致报错。

2.mapper.xml 文件没有放置在正确的位置:在 MyBatis-Plus 中,默认情况下会将 mapper.xml 文件放置在 resources/mapper 目录下。如果 mapper.xml 文件没有放置在该目录下,MyBatis-Plus 将无法找到 mapper.xml 文件,导致报错。

3.mapper.xml 文件没有被正确加载:在 SpringBoot 中,可以使用 MyBatis-Plus 提供的 MapperScannerConfigurer 注解来扫描 mapper 接口,并将其注册到 Spring 容器中。如果没有正确配置该注解,或者 mapper 接口没有被正确注册到 Spring 容器中,MyBatis-Plus 将无法找到 mapper.xml 文件,导致报错。

4.MyBatis-Plus 版本过低:在较早版本的 MyBatis-Plus 中,可能存在无法识别 mapper.xml 文件的 bug,导致报错。此时,可以尝试升级 MyBatis-Plus 版本,或者将 mapper.xml 文件转换为注解形式的 SQL 语句。