MyBatis-Plus找不到Mapper.xml文件的幾種解決方法
在整合SpringBoot和Mybatis-plus時(shí),想寫自定義的sql,所以創(chuàng)建了Mapper.xml文件,但是啟動(dòng)后卻老是報(bào)錯(cuò):
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
很明顯,是Mapper.xml未被掃描到。
此類問題的解決方式實(shí)際上都是配置上有問題,以下列出了一些解決方式。
方式1:Mapper的命名空間和Dao層的接口。
Mapper.xml文件中,<mapper namespace="這里填寫映射的Mapper.java完整路徑,如:com.test.Mapper">
方式2:如果Mapper.xml文件是放到j(luò)ava目錄下,那么在項(xiàng)目的pom.xml文件中需要添加:
<build> ? ? ? ? <resources> ? ? ? ? ? ? <resource> ? ? ? ? ? ? ? ? <directory>src/main/java</directory> ? ? ? ? ? ? ? ? <includes> ? ? ? ? ? ? ? ? ? ? <include>**/*.xml</include> ? ? ? ? ? ? ? ? </includes> ? ? ? ? ? ? </resource> ? ? ? ? </resources> </build>
方式3:注意在.yml配置文件中不要弄混淆Mybatis和Mybatis-plus的配置
比如項(xiàng)目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:如果是多子模塊的項(xiàng)目,Mapper.xml文件是在子模塊項(xiàng)目中,那么記得在classpath后面加*,代表掃描子項(xiàng)目的Mapper.xml文件
mybatis-plus:
mapper-locations: classpath*:com/*/mapper/xml/*.xml
方式5:我最近放的一個(gè)小錯(cuò)誤,就是我的Mapper.xml文件是放在不同層次不同包下的,我配置了很多個(gè)掃描位置:
mybatis-plus:
mapper-locations: classpath*:com/*/mapper/xml/*.xml,com/test/*/mapper/xml/*.xml,/xml/*.xml
配置好發(fā)現(xiàn)依然報(bào)未掃描到Mapper.xml文件,后來發(fā)現(xiàn),我的Mapper.xml文件是放到子模塊中的"com/test/*/mapper/xml"目錄下,配置應(yīng)該要繼續(xù)帶上classpath*:
上面的配置改成:
mybatis-plus:
mapper-locations: classpath*:com/*/mapper/xml/*.xml,classpath*:com/test/*/mapper/xml/*.xml,/xml/*.xml
因?yàn)槁窂角懊娌粠н@個(gè)"classpath*"它默認(rèn)是使用的"classpath",導(dǎo)致掃描不到子模塊項(xiàng)目的Mapper.xml文件。
總結(jié)
到此這篇關(guān)于MyBatis-Plus找不到Mapper.xml文件的幾種解決方法的文章就介紹到這了,更多相關(guān)MyBatis-Plus找不到Mapper.xml文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Security 和Apache Shiro你需要具備哪些條件
這篇文章主要介紹了Spring Security 和Apache Shiro你需要具備哪些條件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Java?Dubbo服務(wù)調(diào)用擴(kuò)展點(diǎn)Filter使用教程
Dubbo是阿里巴巴公司開源的一個(gè)高性能優(yōu)秀的服務(wù)框架,使得應(yīng)用可通過高性能的RPC實(shí)現(xiàn)服務(wù)的輸出和輸入功能,可以和Spring框架無縫集成2022-12-12
java多次嵌套循環(huán)查詢數(shù)據(jù)庫導(dǎo)致代碼中數(shù)據(jù)處理慢的解決
這篇文章主要介紹了java多次嵌套循環(huán)查詢數(shù)據(jù)庫導(dǎo)致代碼中數(shù)據(jù)處理慢的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring?Security中如何獲取AuthenticationManager對(duì)象
有時(shí)需要使用AuthenticationManager(以下簡(jiǎn)稱Manager)對(duì)象,可是這個(gè)對(duì)象不是Bean,沒有直接保存在Spring的Bean庫中,那么如何獲取Spring Security中的這個(gè)對(duì)象呢,需要的朋友可以參考下2022-11-11
使用nacos命名空間namespace用法,測(cè)試時(shí)做實(shí)例隔離
Nacos命名空間用于管理多套不同環(huán)境的服務(wù)器,增加一個(gè)命名空間的概念,可以用一套Nacos注冊(cè)中心管理多套不同的環(huán)境2024-12-12

