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
的基础service
和mapper
进行查询的时候,也就是说没有自己写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算是正常的绑定了。查询也正常了。
分类:
中间件
标签:
mybatis-plus
, mybatis
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构