mybatis-plus: mapper-locations: “classpath*:/mapper/**/*.xml“配置!!!解释

和mybatis一样的道理!!!!如果不指定这个配置,通常要求 XML 映射文件和 Mapper 接口的包名和结构相同!!!!

如果没有配置 mapper-locations,通常文件结构应遵循如下约定:

复制代码
src
├── main
│   ├── java
│   │   └── com
│   │       └── example
│   │           └── mapper
│   │               └── UserMapper.java       # Mapper接口
│   └── resources
│       └── com
│           └── example
│               └── mapper
│                   └── UserMapper.xml       # XML文件

在 MyBatis-Plus 中,mapper-locations 用于指定 Mapper XML 文件的路径,MyBatis-Plus 会根据这个路径找到 XML 映射文件,并自动加载到应用程序中。

1. mapper-locations 的作用

mapper-locations 指定了 MyBatis-Plus 映射文件(*.xml 文件)的加载路径。MyBatis-Plus 会根据这个路径扫描并加载对应的 Mapper XML 文件,这些文件通常包含 SQL 语句(如 selectinsertupdatedelete)的定义,以及 resultMap 等映射关系。

2. 配置项详解

application.yml 文件中,可以通过如下配置来定义 mapper-locations

复制代码
mybatis-plus:
  mapper-locations: "classpath*:/mapper/**/*.xml"

在这里,配置了一个路径模式:

  • classpath*: 表示从类路径中搜索。
  • /mapper/**/*.xml 表示在 mapper 目录下,递归查找所有子目录中符合 *.xml 格式的文件。

例如,如果你的项目结构如下:

复制代码
src
└── main
    └── resources
        └── mapper
            ├── user
            │   └── UserMapper.xml
            └── product
                └── ProductMapper.xml

配置了 classpath*:/mapper/**/*.xml 后,MyBatis-Plus 会自动加载 mapper 文件夹中所有子文件夹(userproduct 等)中的 *.xml 文件。

3. 路径写法说明

不同路径写法的含义如下:

  • classpath:/mapper/*.xml :表示只加载 resources/mapper 文件夹下的所有 .xml 文件(不包括子文件夹)。
  • classpath*:/mapper/**/*.xml :表示递归查找 resources/mapper 文件夹及其所有子文件夹 下的 .xml 文件。
相关推荐
程序员清风15 小时前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
皮皮林55116 小时前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
华仔啊21 小时前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing1 天前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠2 天前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840822 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide2 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
IT探险家2 天前
Java 基本数据类型:8 种原始类型 + 数组 + 6 个新手必踩的坑
java
花花无缺2 天前
搞懂new 关键字(构造函数)和 .builder() 模式(建造者模式)创建对象
java
用户908324602732 天前
Spring Boot + MyBatis-Plus 多租户实战:从数据隔离到权限控制的完整方案
java·后端