這篇文章將為大家詳細講解有關(guān)Mybatis中StatementHandler的作用是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
創(chuàng)新互聯(lián)公司從2013年開始,先為建甌等服務(wù)建站,建甌等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為建甌企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
mybatis-3.4.6.release.
圖1
StatementHandler是對CallableStatement、PreparedStatement、Statement的統(tǒng)一封裝,在JDK中CallableStatement繼承PreparedStatement,PreparedStatement繼承Statement。
這幾個StatementHandler之間有什么區(qū)別呢,我們來看下,RoutingStatementHandler很特殊,使用了委托模式,如List-1
List-1
public class RoutingStatementHandler implements StatementHandler { private final StatementHandler delegate; public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { switch (ms.getStatementType()) { case STATEMENT: delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql); break; case PREPARED: delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql); break; case CALLABLE: delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql); break; default: throw new ExecutorException("Unknown statement type: " + ms.getStatementType()); } }
如List-1,根據(jù)MappedStatement.getStatementType()的值選擇 SimpleStatementHandler、PreparedStatementHandler、CallableStatementHandler。 CallableStatementHandler是處理存儲過程的,而SimpleStatementHandler、PreparedStatementHandler有什么區(qū)別呢?區(qū)別是SimpleStatementHandler處理sql中不含有參數(shù)的,即sql中不含有參數(shù),而PreparedStatementHandler則處理sql中有?的需要預(yù)編譯的sql:
List-2是PreparedStatementHandler的,通過ParameterHandler來設(shè)置參數(shù),List-3是SimpleStatementHandler的,實現(xiàn)為空。
List-2
@Override public void parameterize(Statement statement) throws SQLException { parameterHandler.setParameters((PreparedStatement) statement); }
List-3
@Override public void parameterize(Statement statement) throws SQLException { // N/A }
單獨看StatementHandler,看起來它的方法之間聯(lián)系不起來,我們需要結(jié)合Executor來看,以SimpleExecutor為例,List-4所示,看doQuery方法:
List-4
public class SimpleExecutor extends BaseExecutor { ... @Override publicList doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException { Statement stmt = null; try { Configuration configuration = ms.getConfiguration(); StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql); stmt = prepareStatement(handler, ms.getStatementLog()); return handler. query(stmt, resultHandler); } finally { closeStatement(stmt); } } ... private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException { Statement stmt; Connection connection = getConnection(statementLog); stmt = handler.prepare(connection, transaction.getTimeout()); handler.parameterize(stmt); return stmt; } }
假設(shè)使用的SimpleExecutor、PreparedStatementHandler,SimpleExecutor.doQuery->SimpleExecutor.prepareStatement,
調(diào)用PreparedStatementHandler.prepare方法,得到j(luò)dbc的prepareStatement
PreparedStatementHandler.parameterize方法,見List-2,對prepareStatement進行sql參數(shù)設(shè)置
之后在doQuery方法中,調(diào)用PreparedStatementHandler.query(),如List-5,query方法中再通過ResultSetHandler.handleResultSets來封裝成對象。
List-5
@Override publicList query(Statement statement, ResultHandler resultHandler) throws SQLException { PreparedStatement ps = (PreparedStatement) statement; ps.execute(); return resultSetHandler. handleResultSets(ps); }
Executor->StatementHandler->parameterHandler/ResultSetHandler
針對接口編程,用模板模式,用抽象來降低使用者與具體實現(xiàn)類之間的耦合。
關(guān)于Mybatis中StatementHandler的作用是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。