使用MyBatis架構中經常會看到這樣的異常,今天跟着源碼看一下這條異常的抛出路徑:
先根據路徑找到org.apache.ibatis.binding.BindingException這個異常類(Ctrl + Shift + R沒有直接定位到該類,具體原因待分析),源代碼如圖1
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIwczLcVmds92czlGZvwVP9EUTDZ0aRJkSwk0LcxGbpZ2LcBDM08CXlpXazRnbvZ2LcRlMMVDT2EWNvwFdu9mZvwVPrd1YsZlbh5WNXpVbk1mYox2MMBjVtJWd0ckW65UbM5WOHJWa5kHTvwFd4VGdvwlMvw1ayFWbyVGdhd3P0QDNyUDMxETOxMDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
Ctrl + H中也沒定位到Parameter '__frch_item_0' not found. Available parameter,隻能通過Ctrl + Shift + G一個個檢視BindingException幾個構造函數的調用位置了。
查找一遍後,确定該條異常隻有在org.apache.ibatis.binding.MapperMethod中的内部靜态類ParamMap<V>中抛出
可以看到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