天天看點

通過MyBatis源碼,檢視org.apache.ibatis.binding.BindingException的抛出路徑

使用MyBatis架構中經常會看到這樣的異常,今天跟着源碼看一下這條異常的抛出路徑:

先根據路徑找到org.apache.ibatis.binding.BindingException這個異常類(Ctrl + Shift + R沒有直接定位到該類,具體原因待分析),源代碼如圖1

通過MyBatis源碼,檢視org.apache.ibatis.binding.BindingException的抛出路徑

Ctrl + H中也沒定位到Parameter '__frch_item_0' not found. Available parameter,隻能通過Ctrl + Shift + G一個個檢視BindingException幾個構造函數的調用位置了。

通過MyBatis源碼,檢視org.apache.ibatis.binding.BindingException的抛出路徑

查找一遍後,确定該條異常隻有在org.apache.ibatis.binding.MapperMethod中的内部靜态類ParamMap<V>中抛出

通過MyBatis源碼,檢視org.apache.ibatis.binding.BindingException的抛出路徑

可以看到MyBatis是将Mapper中的參數都封裝成HashMap對象,該内部靜态類是在MapperMethod中的convertArgsToSqlCommandParam方法中進行了調用,

上一級調用為同一個類下的execute(SqlSession sqlSession, Object[] args)方法,

再上一級是org.apache.ibatis.binding.MapperProxy中的invoke(Object proxy, Method method, Object[] args)方法,

再上一步org.apache.ibatis.binding.MapperProxyFactory.newInstance(SqlSession sqlSession),

再往上org.apache.ibatis.binding.MapperRegistry,

在Spring托管MyBatis的項目裡,如何加載初始化MyBatis,進而調用到MapperRegistry

可以參考文章https://www.jianshu.com/p/970120e36acf

繼續閱讀