springboot 项目使用 mybatis 时报错 Property 'mapperLocations' was not specitied
现象
在spring boot项目中,使用mybatis-plus 会出现如下提示。出现该提示,但可能不影响项目的运行。这是怎么回事,该怎么解决呢?
1 | |
分析解决
在解决这个问题之前,要先看一下mybatis的xml文件放的位置。通常有3个地方,如下图:

方案1:放在
1的位置,默认不会加载解析src/main/java/目录下的xml文件,程序会报错,也不符合maven项目规定的。
如果非要这样做需要分别配置pom.xml和application.yml,不建议这样做。- 在pom.xml中添加如下代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**.*</include>
</includes>
</resource>
</resources>
</build> - 在yml配置文件中加上如下配置:
1
2
3
4
5
6
7mybatis-plus:
configuration:
#控制台打印完整带参数SQL语句
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
call-setters-on-nulls: true
# 这里根据自己项目的包修改,扫描到自己的*xml文件
mapper-locations: classpath:xx/xx/xx/**/mapper/*.xml
- 在pom.xml中添加如下代码
方案2:放在
2的位置,并且在application.yml添加配置mapper-locations: classpath*:com/jiguangchao/mybatisplus_01_quickstart/mapper/**/*.xml。
项目编译后会吧xml文件放在和UserMapper的包目录下。把资源文件放在resources目录下是符合maven项目规定的,程序不会报错。
如果不添加mapper-locations配置就会提示Property 'mapperLocations' was not specified.。方案3:放在
3的位置 【推荐】,也就是在resources下建立一个mapper文件夹专门放xml。这样做无需任何配置,也不提示Property 'mapperLocations' was not specified.。
因为mybatis-plus的mapper-locations的默认值就是classpath*:/mapper/**/*.xml。
springboot 项目使用 mybatis 时报错 Property 'mapperLocations' was not specitied
https://flepeng.github.io/021-Java-71-问题-02-springboot-springboot-项目使用-mybatis-时报错-Property-mapperLocations-was-not-specitied/