Springcloud多模組整合mybatis-plus
最近打算搭一個spring-cloud的框架,並打算整合mybatis-plus的外掛。然後卻遇到了一個消耗了我十幾個鐘的問題。
出現的問題是:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bestlmc.lihuamao.services.service.ITMenuService.getTest
這裡顯示的是繫結的問題,然後我查了一下官方文件,一般是以下幾個問題導致:
https://mp.baomidou.com/guide/faq.html#出現-invalid-bound-statement-not-found-異常
-
檢查是不是引入 jar 衝突
-
檢查 Mapper.java 的掃描路徑
-
方法一:在
Configuration
類上使用註解MapperScan
-
方法二:在
Configuration
類裡面,配置MapperScannerConfigurer
(
-
-
檢查是否指定了主鍵?如未指定,則會導致
selectById
相關 ID 無法操作,請用註解@TableId
註解表 ID 主鍵。當然@TableId
註解可以沒有!但是你的主鍵必須叫 id(忽略大小寫) -
SqlSessionFactory不要使用原生的,請使用MybatisSqlSessionFactory
-
檢查是否自定義了SqlInjector,是否複寫了getMethodList()方法,該方法裡是否注入了你需要的方法(可參考DefaultSqlInjector)
也有人說配置檔案中,mybatis要改成mybatis-plus:
mybatis-plus
諸如此類,終究還是沒有效果。
但是我自己用springboot但模組整合mybatis-plus使用的話還是正常的。然而,這跟我想要的效果不一樣。出於解耦的思想,我想要將service層和mapper層的程式碼分離出來,作為一個公用的模組。這樣便可以減少程式碼的冗餘。
只是按照這樣的模組搭建,能成功執行,訪問介面的時間就會報org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
這樣的錯誤。
最終檢查多次之後,發現了問題出在啟動類中:
@MapperScan
只是掃描mapper的實現類
這裡應該使用的是@ComponentScan
,@ComponentScan
適合掃描各類的been。
最終將MapperScan更換為ComponentScan,解決問題。
詳細程式碼可以檢視我的開源專案https://gitee.com/bestlmc/lihuamao_blog