这个问题上卡了我五个小时。
问题起因:在springboot框架的项目。在controller层通过注解引入service层的接口时出现Invalid bound statement (not found)错误。网上搜索搜索了很多办法,下面列出来
拼写错误:因为一些名称,id错误导致两者不能映射 这些检查和修正的工作自己
- dao层的方法和mapper.xml中的方法不一样;
- mapper中的namespace resultParameter 和对应的dao层entity层不一样
- 拼写错误 如漏写 少写 多写…
其实这个问题很容易排查,装一个dao层和mapper层跳转的插件就行,如果有拼写类的错误,肯定是无法跳转的。idea环境下的话Free mybatis plugin就挺不错。
文件配置原因:spring配置文件中关于mybatis的与xml文件路径寻找相关的配置没有写。导致调用时找不到文件对应文件的路径,也就是说文件扫描问题。
解决办法一
Mybatis文件配置问题。在springboot的配置文件application.yml中加入下面的代码,
# MybatisPlus 配置
mybatis-plus:
mapper-locations: classpath:mapper/*/*Mapper.xml
解决办法二
在pom.xml文件中加入下面的代码。一般来说,service层出现Invalid bound statement错误应该可以解决了。
<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层报错“Invalid bound statement”的问题,上面的方法也不能解决我的问题。
我的解决办法:原来报错的时候我是在controller层注解调用service层接口。
后来改成直接注解调用serviceImpl实现,而不是service接口。问题就解决了