扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章将为大家详细讲解有关Mybatis中StatementHandler的作用是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联公司从2013年开始,先为建瓯等服务建站,建瓯等地企业,进行企业商务咨询服务。为建瓯企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
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 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; } }
假设使用的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 publicList query(Statement statement, ResultHandler resultHandler) throws SQLException { PreparedStatement ps = (PreparedStatement) statement; ps.execute(); return resultSetHandler. handleResultSets(ps); }
Executor->StatementHandler->parameterHandler/ResultSetHandler
针对接口编程,用模板模式,用抽象来降低使用者与具体实现类之间的耦合。
关于Mybatis中StatementHandler的作用是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流