• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

mybatis plus解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)方法

武飞扬头像
spark_cat
帮助1

总结解决 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)方法

问题背景:在做SpringBoot项目的时候,通过controller层调用service层的接口时出现了如下的报错

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.example.service.UserService.getById
	at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:235) ~[mybatis-3.5.2.jar:3.5.2]
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.<init>(MybatisMapperMethod.java:49) ~[mybatis-plus-core-3.2.0.jar:3.2.0]
	at com.example.controller.UserController.obj(UserController.java:18) ~[classes/:na]

1.问题原因

通过网上搜索到的报错原因,本质原因是由于dao层(也可以叫做mapper接口)跟mapper.xml文件没有映射,而大部分的原因有如下的几种类型:

  1. dao层的方法跟mapper.xml的方法不一样
  2. mapper.xml中的namespace 要写对应的dao层和entity层不一样
  3. spring配置文件中mybatis与xml文件路径的配置没有写,导致无法映射成功
  4. 拼写错误导致

2.解决方法

2.1方法1:

Mypatis配置文件有问题,在application.yml添加如下代码:

mybatis-plus:
  mapper-locations: classpath*:com/example/mapper/xml/*Mapper.xml

mapper-locations的路径要对应响应 .xml 文件所在路径

2.2方法2:

在pom.xml 文件下加入如下代码:

<build>	
	<resources>
		<resource>
			<directory>src/main/java</directory>
			<includes>
				<include>**/*.properties</include>
				<include>**/*.xml</include>
			</includes>
			<filtering>false</filtering>
		</resource>
		<resource>
			<directory>src/main/resources</directory>
			<includes>
				<include>**/*.properties</include>
				<include>**/*.xml</include>
			</includes>
			<filtering>false</filtering>
		</resource>
	</resources>
</build>
学新通

这上部分的解决方法应该能解决大部分的service层报错的问题,但是这上面的方法并不能解决我的问题。通过我继续对其研究,发现在我的一个扫描Mapper配置文件中路径错误

学新通
这是由于配置的base-package范围太大,导致service层的接口也被包装了,将base-package的范围缩小到dao层既可以解决了
学新通
将其修改到对应的dao层路径后问题就得以解决了

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhghaiec
系列文章
更多 icon
同类精品
更多 icon
继续加载