解决Mybatis 报错Invalid bound statement (not found)

2021/10/23 23:40:58

本文主要是介绍解决Mybatis 报错Invalid bound statement (not found),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

解决Mybatis 报错Invalid bound statement (not found)

出现此错误的原因

1.xml文件不存在

2.xml文件和mapper没有映射上

  • namespace指定映射mapper的路径错误
  • id和mapper中的方法名不一致

image-20211023230835681

3.xml文件在java目录下而不在resource目录下,因此生成target中无xml

场景

​ 在使用Mybatis-plus框架时,自定义mapper接口和xm文件时,由于使用的是MP的自动生成代码插件,导致mapper接口和xml文件都在java目录下,而在编译时,在java路径下的xml文件不会被自动编译进去,编译只会识别.java文件,只有在resource下的xml文件在打包时才能编译进去。

下图是MP自动生成代码插件的xml和mapper目录(不再resource中)

image-20211023225723524

而编译出来的target目录下是这样的:

image-20211023230007719

解决方法:

1.在pom文件中添加

    <build>
        <!-- 项目打包时会将java目录中的*.xml文件也进行打包 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

2.手动将java目录下的xml文件移到resource目录下,并且在Spring Boot中的配置文件中加入

mybatis-plus:
	mapper-locations: classpath:**/*.xml //classpath后添加你xml文件的目录

注意:博主血的教训!!!

mapper-locations中的目录一定要是你放置xml文件的目录一致,否则就算target中存在xml文件,也会出现这个错误!!!

由于本人能力有限,欢迎访问个人博客,进行技术交流,如有不足,欢迎指正~



这篇关于解决Mybatis 报错Invalid bound statement (not found)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程