这篇文章将为大家详细讲解有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。