FileNotFoundException: class path resource [mapper/*.xml] cannot be opened because it does not exist

场景:
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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值