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 语句(如 select、insert、update、delete)的定义,以及 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 文件夹中所有子文件夹(user、product 等)中的 *.xml 文件。

3. 路径写法说明

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

  • classpath:/mapper/*.xml :表示只加载 resources/mapper 文件夹下的所有 .xml 文件(不包括子文件夹)。
  • classpath*:/mapper/**/*.xml :表示递归查找 resources/mapper 文件夹及其所有子文件夹 下的 .xml 文件。
相关推荐
这个昵称不容易15 分钟前
靓车汽车销售网站设计与实现:SpringBoot 汽车销售 203 个接口,附论文与运行截图
java·springboot·毕设
真上帝的左手33 分钟前
7. 网络安全-网关-认证鉴权实战-Spring Boot+SpringSecurity+JWT登录认证
java·spring boot·web安全
Resean022310 小时前
架构设计系列(十六):DDD 与代码架构
java·开发语言·架构
学长毕设11 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
Flynt13 小时前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
她的男孩16 小时前
Spring Boot 接 Flowable:请假单要同时走财务和行政,包容分支和默认分支怎么配
java·spring boot·工作流引擎
咖啡八杯17 小时前
LoginUser 适配器模式:适配 SysUser 到 UserDetails
java·开发语言·后端·框架·适配器模式·若依
北冥有鱼18 小时前
从 bean::method 里抠出注解:Java 方法引用元数据反查全记录
java·后端
CoderYanger18 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis
玖米GEO运营王总监1 天前
抖音和视频号算法区别:玖米快跑的双端分发信号与内容适配对比
java·前端·人工智能