MyBatis-Plus找不到Mapper.xml文件的幾種解決方法
在整合SpringBoot和Mybatis-plus時,想寫自定義的sql,所以創(chuàng)建了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,其中已經(jīng)包含了Mybatis了,配置文件中最好統(tǒng)一寫成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
配置好發(fā)現(xiàn)依然報未掃描到Mapper.xml文件,后來發(fā)現(xiàn),我的Mapper.xml文件是放到子模塊中的"com/test/*/mapper/xml"目錄下,配置應該要繼續(xù)帶上classpath*:
上面的配置改成:
mybatis-plus:
mapper-locations: classpath*:com/*/mapper/xml/*.xml,classpath*:com/test/*/mapper/xml/*.xml,/xml/*.xml
因為路徑前面不帶這個"classpath*"它默認是使用的"classpath",導致掃描不到子模塊項目的Mapper.xml文件。
總結(jié)
到此這篇關于MyBatis-Plus找不到Mapper.xml文件的幾種解決方法的文章就介紹到這了,更多相關MyBatis-Plus找不到Mapper.xml文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決springboot MultipartFile文件上傳遇到的問題
本文給大家?guī)砹私鉀Qspringboot MultipartFile文件上傳遇到的問題,解決方法超簡單,感興趣的朋友參考下本文2018-08-08Spring security實現(xiàn)對賬戶進行加密
這篇文章主要介紹了Spring security實現(xiàn)對賬戶進行加密,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03Java輕松實現(xiàn)批量插入或刪除Excel行列操作
在職場生活中,對Excel工作表的行和列進行操作是非常普遍的需求,下面小編就來和大家介紹一下如何在Java中完成批量插入、刪除行和列的操作吧2023-10-10MyBatis Plus 將查詢結(jié)果封裝到指定實體的方法步驟
這篇文章主要介紹了MyBatis Plus 將查詢結(jié)果封裝到指定實體的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09SpringBoot 多任務并行+線程池處理的實現(xiàn)
這篇文章主要介紹了SpringBoot 多任務并行+線程池處理的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04