场景:
Spring Boot 整合 Mybatis 框架,找不到资源文件。
错误原因:
出现 java.io.FileNotFoundException: class path resource [mapper/*.xml] cannot be opened because it does not exist 错误,通常是因为 MyBatis 找不到指定的 XML Mapper 文件。
1. 检查资源目录以及检查 XML 文件的命名
注意目录地址以及拼写,实际中就见过目录名称拼写错误的。
注意文件名称拼写是否正确!!!
例如:
src
└── main
├── java
│ └── com
│ └── example
│ ├── model
│ └── mapper
│ └── UserMapper.java
└── resources
└── mapper
└── UserMapper.xml
2. 检查配置文件中的配置信息
配置 MyBatis 的 XML Mapper 文件的路径应该是
mybatis.mapper-locations
而不是mybatis.config-location
实际就见过配置错误的。
如果存在多个配置文件,要注意配置信息重复
导致的冲突。
mybatis.mapper-locations=classpath:mapper/*.xml
3. 检查Maven资源构建配置正确
如果你在 IDE 中运行项目,确认 resources 目录下的文件会被包含在构建过程中。你可以在 Maven 构建后查看生成的 JAR 文件,确认 mapper 目录和 XML 文件是否被打包。
例如:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>