irpas技术客

MyBatis-Plus找不到Mapper.xml文件的解决方法_Stone Lio_mybatisplus找不到mapper

大大的周 2978

在整合SpringBoot和Mybatis-plus时,想写自定义的sql,所以创建了Mapper.xml文件,但是启动后却老是报错:

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

很明显,是Mapper.xml未被扫描到。

此类问题的解决方式实际上都是配置上有问题,以下列出了一些解决方式。

方式1:Mapper的命名空间和Dao层的接口。

Mapper.xml文件中,<mapper namespace="这里填写映射的Mapper.java完整路径,如:com.test.Mapper">

方式2:如果Mapper.xml文件是放到java目录下,那么在项目的pom.xml文件中需要添加:

<build>

????????<resources> ?? ??? ??? ?<resource> ?? ??? ??? ??? ?<directory>src/main/java</directory> ?? ??? ??? ??? ?<includes> ?? ??? ??? ??? ??? ?<include>**/*.xml</include> ?? ??? ??? ??? ?</includes> ?? ??? ??? ?</resource> ?? ??? ?</resources>

</build>

方式3:注意在.yml配置文件中不要弄混淆Mybatis和Mybatis-plus的配置

比如项目pom.xml中引用的是Mybatis-plus的starter,其中已经包含了Mybatis了,配置文件中最好统一写成Mybatis-plus的配置:

mybatis:

? ? mapper-locations: classpath:com/*/mapper/xml/*.xml

改成

mybatis-plus:

? ??mapper-locations: classpath:com/*/mapper/xml/*.xml

方式4:如果是多子模块的项目,Mapper.xml文件是在子模块项目中,那么记得在classpath后面加*,代表扫描子项目的Mapper.xml文件

mybatis-plus:

? ??mapper-locations: classpath*:com/*/mapper/xml/*.xml

方式5:我最近放的一个小错误,就是我的Mapper.xml文件是放在不同层次不同包下的,我配置了很多个扫描位置:

mybatis-plus:

? ??mapper-locations: classpath*:com/*/mapper/xml/*.xml,com/test/*/mapper/xml/*.xml,/xml/*.xml

配置好发现依然报未扫描到Mapper.xml文件,后来发现,我的Mapper.xml文件是放到子模块中的"com/test/*/mapper/xml"目录下,配置应该要继续带上classpath*:

上面的配置改成:

mybatis-plus:

? ??mapper-locations: classpath*:com/*/mapper/xml/*.xml,classpath*:com/test/*/mapper/xml/*.xml,/xml/*.xml

因为路径前面不带这个"classpath*"它默认是使用的"classpath",导致扫描不到子模块项目的Mapper.xml文件。


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #invalid #bound #statement #not #found