mapper-locations配置子路径
时间: 2023-09-23 19:03:35 浏览: 47
在 MyBatis 中,我们可以在 `mapper.xml` 文件中使用 `mapper-locations` 属性来指定 Mapper 接口对应的 SQL 语句文件的位置。如果这些 SQL 语句文件不在 `classpath` 下,我们可以使用相对于 `mybatis-config.xml` 文件的路径或绝对路径来指定它们的位置。
如果在 `mapper-locations` 中指定了子路径,那么实际上是相对于 `mybatis-config.xml` 文件所在的目录来解析的。例如,我们有如下的目录结构:
```
mybatis-config.xml
├── mapper
│ ├── UserMapper.xml
│ └── OrderMapper.xml
└── mapper-sub
├── UserMapper.xml
└── OrderMapper.xml
```
如果我们要在 `mybatis-config.xml` 中加载 `mapper-sub` 目录下的所有 Mapper 文件,可以这样配置:
```xml
<configuration>
<mappers>
<mapper resource="mapper-sub/*.xml"/>
</mappers>
</configuration>
```
这里的 `resource` 属性指定了相对于 `mybatis-config.xml` 文件所在目录的路径,因此实际上会加载 `mapper-sub` 目录下的所有 `.xml` 文件。如果使用绝对路径,也是同样的道理。