Maven 项目不打包 *.xml 映射文件 的问题解决

本文介绍如何在Maven项目中配置pom.xml文件,确保项目能够正确包含src/main/java目录下的XML文件及src/main/resources目录下的XML和Properties文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<think>我们正在解决如何配置SpringBoot中MyBatis的mapper-locations路径问题。用户当前的mapper文件位置:src/main/java/com/htsc/mdc/uniquery/server/mapper/*.xml在SpringBoot中,我们通常在application.properties或application.yml中配置mybatis.mapper-locations。注意:由于Maven默认只打包src/main/resources下的资源文件,所以放在src/main/java下的xml文件在打包时默认会被打包到classpath中。因此,我们除了配置mapper-locations,还需要在pom.xml中配置资源打包规则。步骤:1.配置pom.xml,确保src/main/java下的xml文件被打包到classpath中。2.在application.properties或application.yml中配置mybatis.mapper-locations。引用:引用[2]提供了如何配置pom.xml打包src/main/java下的xml文件。引用[3]和[5]解释了mapper-locations的配置方式,以及通配符的使用。具体操作:第一步:配置pom.xml,将src/main/java下的xml文件作为资源打包(参考引用[2])```xml<build><resources><resource><directory>src/main/resources</directory></resource><!--这一部分是为了将src/main/java下的xml文件也打包进classpath--><resource><directory>src/main/java</directory><includes><include>**/*.xml</include></includes><filtering>false</filtering></resource></resources></build>```第二步:在application.yml中配置mapper-locations(使用通配符)用户给出的路径:src/main/java/com/htsc/mdc/uniquery/server/mapper/*.xml注意:在classpath中,这个路径实际上对应的是类路径下的:com/htsc/mdc/uniquery/server/mapper/目录。配置方式:```ymlmybatis:mapper-locations:classpath*:com/htsc/mdc/uniquery/server/mapper/*.xml```注意:这里使用了`classpath*:`,表示仅从当前项目的classpath中查找,还从依赖的jar中查找(如果有的话)。但如果是项目自己的mapper,通常用`classpath:`即可。说明:-`classpath:`:只从当前项目的classpath中查找。-`classpath*:`:仅从当前项目的classpath中查找,还从所有依赖的jar包中查找。考虑到通常我们的mapper文件都在当前项目中,所以也可以使用:```ymlmybatis:mapper-locations:classpath:com/htsc/mdc/uniquery/server/mapper/*.xml```但注意:在配置路径时,路径中的分隔符是正斜杠`/`,而且使用包路径时,要忘记通配符`*.xml`。第三步:验证启动项目,确保MyBatis能够正确加载这些映射文件。可能遇到的问题:1.如果出现找到mapper.xml的错误,请检查:-pom.xml中的资源打包配置是否正确-mapper-locations的路径是否正确(注意在打包后,xml文件会在classpath下的对应包路径中)-使用classpath*:和classpath:的差异(如果项目有多个模块,可能需要classpath*:)引用解释:-引用[3]中给出的配置是:`classpath*:com/znxk/basic/**/dao/mapping/*.xml`,其中使用了双星号(**)表示任意深度的子目录。-我们的路径是:`com/htsc/mdc/uniquery/server/mapper/*.xml`,是固定目录,所以需要双星号。额外说明:如果使用SpringBoot配置文件(properties格式),则这样配置:```propertiesmybatis.mapper-locations=classpath:com/htsc/mdc/uniquery/server/mapper/*.xml```相关问题:1.在SpringBoot中如何配置多个mapper-locations路径?2.如果XML映射文件和Mapper接口在同一位置,如何配置?3.使用MyBatis时,如何避免XML文件打包后路径错误的问题?下面将生成相关问题。</think> ### 如何在 Spring Boot 中正确配置 MyBatis 的 mapper-locations #### 1. **mapper-locations 配置解析** 针对您的需求:`src/main/java/com/htsc/mdc/uniquery/server/mapper/*.xml` ```yaml mybatis: mapper-locations: - classpath:com/htsc/mdc/uniquery/server/mapper/*.xml ``` **配置说明**: - `classpath:` 表示从类路径(编译后的 `target/classes`)开始扫描 - `/com/htsc/mdc/uniquery/server/mapper/` 是精确的包路径 - `*.xml` 通配符匹配该目录下的所有 XML 文件 实际加载路径等价于: ``` src/main/resources/com/htsc/mdc/uniquery/server/mapper/*.xml 或 target/classes/com/htsc/mdc/uniquery/server/mapper/*.xml ``` #### 2. **关键配置注意事项** 1. **路径分隔符** 必须使用 `/` 代替 `.`(Java 包名转换为路径格式) ```diff - classpath:com.htsc.mdc.uniquery.server.mapper*.xml // 错误 + classpath:com/htsc/mdc/uniquery/server/mapper/*.xml // 正确 ``` 2. **打包资源配置**(必需) 在 `pom.xml` 中添加资源声明,确保 XML打包到类路径: ```xml <build> <resources> <!-- 扫描 src/main/java 下的 XML --> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <!-- 标准 resources 目录 --> <resource> <directory>src/main/resources</directory> </resource> </resources> </build> ``` [引用参考:打包配置][^2] 3. **通配符特殊用法** | 通配符 | 含义 | 使用示例 | |-------------|-----------------------------|---------------------------------| | `*.xml` | 匹配单个目录下的 XML 文件 | `mapper/*.xml` | | `**/*.xml` | 递归匹配多级目录的 XML 文件 | `com/**/mapper/*.xml` | | `classpath*:` | 搜索所有类路径(含依赖库) | `classpath*:com/**/mapper/*.xml` | 4. **目录结构验证** 确保编译后的目录结构为: ``` target/classes └── com └── htsc └── mdc └── uniquery └── server └── mapper ├── UserMapper.xml └── OrderMapper.xml ``` #### 3. **最佳实践建议** 1. **推荐目录分离**XML 放在 `src/main/resources` 下对应包路径: ``` src/main/resources/com/htsc/mdc/uniquery/server/mapper/ ``` 配置简化为: ```yaml mybatis: mapper-locations: classpath:com/htsc/mdc/uniquery/server/mapper/*.xml ``` 2. **动态 SQL 检测** 在 `application.yml` 添加验证配置: ```yaml mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl ``` 启动时观察日志是否打印: ``` Parsed mapper file: file [com/htsc/mdc/uniquery/server/mapper/UserMapper.xml] ``` 3. **目录结构优化** 建议将 XML 组织为: ``` src/main/resources └── mapper └── uniquery ├── user │ └── UserMapper.xml └── order └── OrderMapper.xml ``` 使用递归匹配: ```yaml mapper-locations: classpath:mapper/uniquery/**/*.xml ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值