和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文件。