1. RoutingStatementHandler类
该类的构造函数代码如下所示:
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());
}
}
这里使用了策略模式,MappedStatement 参数的statementType为必传参数,通过该参数进行实例化策略类。StatementHandler为抽象策略类,SimpleStatementHandler、PreparedStatementHandler、CallableStatementHandler为具体策略类,RoutingStatementHandler为环境类。
除了使用到了策略模式外,还用到了模板方法模式,抽象模板类是BaseStatementHandler,模板方法是prepare,该方法是获取Statement接口的对象的,抽象方法是instantiateStatement,具体方法setStatementTimeout、setFetchSize、closeStatement。
2. 枚举类StatementType
public enum StatementType {
STATEMENT, PREPARED, CALLABLE
}
枚举类StatementType 分别对应普通的Statement、PreparedStatement、CallableStatement,关联的处理器就是SimpleStatementHandler、PreparedStatementHandler、CallableStatementHandler,当需要执行存储过程时,将会使用CallableStatementHandler。
网友评论