您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Mybatis中StatementHandler的作用是什么,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
mybatis-3.4.6.release.
圖1
StatementHandler是對CallableStatement、PreparedStatement、Statement的統一封裝,在JDK中CallableStatement繼承PreparedStatement,PreparedStatement繼承Statement。
這幾個StatementHandler之間有什么區別呢,我們來看下,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,根據MappedStatement.getStatementType()的值選擇 SimpleStatementHandler、PreparedStatementHandler、CallableStatementHandler。 CallableStatementHandler是處理存儲過程的,而SimpleStatementHandler、PreparedStatementHandler有什么區別呢?區別是SimpleStatementHandler處理sql中不含有參數的,即sql中不含有參數,而PreparedStatementHandler則處理sql中有?的需要預編譯的sql:
List-2是PreparedStatementHandler的,通過ParameterHandler來設置參數,List-3是SimpleStatementHandler的,實現為空。
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,看起來它的方法之間聯系不起來,我們需要結合Executor來看,以SimpleExecutor為例,List-4所示,看doQuery方法:
List-4
public class SimpleExecutor extends BaseExecutor { ... @Override public <E> List<E> 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.<E>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; } }
假設使用的SimpleExecutor、PreparedStatementHandler,SimpleExecutor.doQuery->SimpleExecutor.prepareStatement,
調用PreparedStatementHandler.prepare方法,得到jdbc的prepareStatement
PreparedStatementHandler.parameterize方法,見List-2,對prepareStatement進行sql參數設置
之后在doQuery方法中,調用PreparedStatementHandler.query(),如List-5,query方法中再通過ResultSetHandler.handleResultSets來封裝成對象。
List-5
@Override public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException { PreparedStatement ps = (PreparedStatement) statement; ps.execute(); return resultSetHandler.<E> handleResultSets(ps); }
Executor->StatementHandler->parameterHandler/ResultSetHandler
針對接口編程,用模板模式,用抽象來降低使用者與具體實現類之間的耦合。
關于Mybatis中StatementHandler的作用是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。