StatementHandler是MyBatis四大核心對象之一,是專門處理數(shù)據(jù)庫匯報的組件,封裝了JDBC Statement操作,負(fù)責(zé)對JDBC statement 的操作。
1.Executor繼承體系

如上圖所示,StatementHandler分別有兩個實現(xiàn)類?BaseStatementHandler?和RoutingStatementHandler,BaseStatementHandler 有三個實現(xiàn)類, 他們分別是 SimpleStatementHandler、PreparedStatementHandler 和 CallableStatementHandler。
RoutingStatementHandler?: RoutingStatementHandler 并沒有對 Statement 對象進行使用,只是根據(jù)StatementType 來創(chuàng)建一個代理,代理的就是BaseStatementHandler的三種實現(xiàn)類。在MyBatis工作時,使用的StatementHandler 接口對象實際上就是 RoutingStatementHandler 對象。
BaseStatementHandler?: 是 StatementHandler 接口的另一個實現(xiàn)類,它本身是一個抽象類,用于簡化StatementHandler 接口實現(xiàn)的難度,屬于適配器設(shè)計模式體現(xiàn),它主要有三個實現(xiàn)類
SimpleStatementHandler: 管理 Statement 對象并向數(shù)據(jù)庫中推送不需要預(yù)編譯的SQL語句。
PreparedStatementHandler: 管理 Statement 對象并向數(shù)據(jù)中推送需要預(yù)編譯的SQL語句。
CallableStatementHandler:管理 Statement 對象并調(diào)用數(shù)據(jù)庫中的存儲過程。
SimpleStatementHandler 和 PreparedStatementHandler 的區(qū)別是 SQL 語句是否包含變量,是否通過外部進行參數(shù)傳入。SimpleStatementHandler 用于執(zhí)行沒有任何參數(shù)傳入的 SQL,PreparedStatementHandler 需要對外部傳入的變量和參數(shù)進行提前參數(shù)綁定和賦值。
2.StatementHandler 的創(chuàng)建和源碼分析
我們繼續(xù)來上篇《MyBatis四大核心對象之Executor》的query?的調(diào)用鏈路,StatementHandler 的創(chuàng)建過程如下


