Property 'mapperLocations' was not specified和Invalid bound statement (not found)

问题背景

使用mybatis-plus依赖进行springboot项目开发过程中遇到的问题

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>

上面是使用的依赖。
在使用mybatis-plus的基础servicemapper进行查询的时候,也就是说没有自己写SQL进行查询的时候是没有问题的,可以正常的去数据库查询并获取数据。
但是注意,在服务启动过程中已经打印了一行异常日志:

Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Property 'mapperLocations' was not specified

翻译成中文就是:未指定属性“mapperLocations”
然后当我在.xml文件里手写SQL并进行查询的时候,发现报错了,查询不了。主要信息就是下面的:

Invalid bound statement (not found)

翻译成中文是:无效的绑定语句(未找到)
显而易见原因是mapper没有和.xml文件绑定。

原因及解决方法

造成上面问题的原因是配置

# mybatis-plus配置
mybatis-plus:
mapper-locations: classpath*:/mapper/*/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
banner: false

上面是我原本的对mybatis-plus的配置,mapper-locations这条属性的意义上当你的.xml文件不放在resources目录下时,你需要指定目录。当你的.xml文件放在resources目录下时,不需要指定,因为默认值就是指定resource目录下:

private String[] mapperLocations = new String[]{"classpath*:/mapper/**/*.xml"};

所以,错误原因是:指定了错误的mapperLocations(比默认值少了个*,匹配不上了)。
当我删除这一条配置之后。服务启动的日志是:

Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Parsed mapper file: 'file [D:\coding_work\Code\my_code\xx\target\classes\mapper\UrlMapper.xml]'
Parsed mapper file: 'file [D:\coding_work\Code\my_code\xx\target\classes\mapper\UrlTypeMapper.xml]'

这才是正确的日志,此时mapper算是正常的绑定了。查询也正常了。

posted @   大唐冠军侯  阅读(2537)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示