解决Spring boot整合mybatis,xml资源文件放置及路径配置问题
作者:babyyaoyao 时间:2021-07-07 12:27:20
网上各种解决方案,我试了好久,整合了几篇文章才凑出来,在这里分享一下,实在不想网友们在这里面绕圈子,毕竟,写代码的时间是愉快的,解决bug也是愉快的,但也是一直在bug里面绕圈子就不爽了。
亲自试验:
1) 我的mapper和xml是这样子放置的
2) 在.xml中namespace是这样的:
3) application.properties中mybatis.mapper-locations得这么配置到xml
4) 最后呢,你只要在pom.xml中build下这样配置
5) 按照我这种位置防止,把响应的包名换成自己的,就基本上不会没有什么问题的。
也不用去加什么*Application中去加什么注解@MapperScan(value = "com.acme.shop.mapper")什么的了,不需要呢。
补充知识:mybatis-plus的 mapper.xml 路径配置的坑
问题:
在SpringBoot整合MyBatis时,若mapper映射文件在java路径下,就会一直报错Invalid bound statement (not found): xxx.xxxMapper.selectxx
说明Mapper接口是可以调用到的,问题就出在无法映射到Mapper.xml文件。
同时查看输出target目录下,没有xxxmapper.xml文件的编译结果。
原因:
SpringBoot默认配置情况下,只有resources路径下的xml文件会被扫描。
解决办法:
方法一:【推荐使用】
将mapper映射文件放在resources目录下,通常会在resources目录下建一个mapper目录,存放各种mapper的映射文件,
并且在application.properties中配置 Mapper映射文件的路径为:
#指定myBatis的核心配置文件与Mapper映射文件
mybatis.mapper-locations=classpath:mapper/*.xml
测试成功,查看target目录下,有xxxMapper.xml文件的编译结果。
方法二:
mapper映射文件依旧放在java目录下,
此时,需要在pom.xml文件中添加java路径下xml文件扫描配置,
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/java</directory>
<excludes>
<exclude>**/*.xml</exclude>
</excludes>
</resource>
</resources>
</build>
并且在application.properties中配置 Mapper映射文件的路径时要加上包名,
#mybatis.mapper-locations=classpath:包名/*.xml
mybatis.mapper-locations=classpath:org.xiao.dao/*.xml
测试成功,查看target目录下,有xxxMapper.xml文件的编译结果。
来源:https://blog.csdn.net/babyyaoyao/article/details/81096923
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringCloud Feign配置应用详细介绍
![](https://img.aspxhome.com/file/2023/7/57497_0s.png)
maven多个仓库查询的优先级顺序案例讲解
c# 调用Surfer软件,添加引用的具体操作方法
![](https://img.aspxhome.com/file/2023/6/68656_0s.png)
Spring Boot Admin 进行项目监控管理的方法
![](https://img.aspxhome.com/file/2023/1/67001_0s.png)
Java8使用stream实现list中对象属性的合并(去重并求和)
![](https://img.aspxhome.com/file/2023/9/62379_0s.png)
java web服务器实现跨域访问
Java卡片布局管理器解释及实例
如何使用Jenkins构建GIT+Maven项目
![](https://img.aspxhome.com/file/2023/9/61839_0s.png)
流读取导致StringBuilder.toString()乱码的问题及解决
java前后端加密解密crypto-js的实现
Java如何实现压缩文件与解压缩zip文件
![](https://img.aspxhome.com/file/2023/6/67496_0s.png)
入门JDK集合之HashMap解析
![](https://img.aspxhome.com/file/2023/1/59901_0s.png)
细谈java同步之JMM(Java Memory Model)
![](https://img.aspxhome.com/file/2023/3/59973_0s.png)
Java定位问题线程解析
![](https://img.aspxhome.com/file/2023/1/58041_0s.png)
Java Swing实现窗体添加背景图片的2种方法详解
![](https://img.aspxhome.com/file/2023/1/62231_0s.jpg)
java对ArrayList排序代码示例
一篇文章带你了解JVM垃圾回收
![](https://img.aspxhome.com/file/2023/9/61769_0s.png)
java使用@Scheduled注解执行定时任务
![](https://img.aspxhome.com/file/2023/6/65786_0s.jpg)
Hutool开发MapUtil工具类使用示例
![](https://img.aspxhome.com/file/2023/3/63693_0s.jpg)