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 文件。
相关推荐
wno70410 小时前
Centos下PostgreSQL安装部署详细步骤
java·数据库·spring boot
nice_lcj52010 小时前
【从零理解计算机网络 · 04】TCP 三次握手:为什么恰好是三次
java·后端·tcp/ip·计算机网络·面试·tcp·三次握手
Sarvartha11 小时前
反射综合案例:SQL 语句生成
java·开发语言
Resean022321 小时前
Spring 源码分析(二)配置类的解析与扫描
java·后端·spring
小毛球.21 小时前
第 3 篇:IDEA 最新版下载与完整安装教程(Mac)
java·macos·intellij-idea·idea
Misnearch1 天前
ScheduledExecutorService
java·线程池
ashiho1 天前
【LeetCode hot 100】3. 无重复字符的最长子串 (三种解法)
java·数据结构·后端·算法·leetcode·滑动窗口
T_Wizard1 天前
AI 回复为什么要转圈 3 秒才「啪」一下全出来?把流式输出拆开看,顺便踩了 6 个坑
java·ai编程
CoderYanger1 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第二弹)
java·数据库·面试·职场和发展·java-ee·tomcat·mybatis
小满、1 天前
Java 值传递
java·java 值传递