概述
StatementHandler,Statement处理器,主要是和jdbc中的Statement交互。
public interface StatementHandler {
/**
* 创建Statement
* @param connection
* @param transactionTimeout
* @return
* @throws SQLException
*/
Statement prepare(Connection connection, Integer transactionTimeout)
throws SQLException;
/**
* 设置参数到statement
* @param statement
* @throws SQLException
*/
void parameterize(Statement statement)
throws SQLException;
/**
* 批量支持
* @param statement
* @throws SQLException
*/
void batch(Statement statement)
throws SQLException;
int update(Statement statement)
throws SQLException;
<E> List<E> query(Statement statement, ResultHandler resultHandler)
throws SQLException;
<E> Cursor<E> queryCursor(Statement statement)
throws SQLException;
BoundSql getBoundSql();
ParameterHandler getParameterHandler();
}
提供的能力:
- 创建Statement实例
- 设置参数
- 执行sql
类图结构:
看这个结构和Executor是不是特别像。
BaseStatementHandler依旧是通用逻辑处理(异常处理、通用参数设置)
SimpleStatementHandler:普通Statement处理器。
PreparedStatementHandler:PrepareStatement处理器。
CallableStatementHandler:CallableStatement处理器。
RoutingStatementHandler:封装路由逻辑。
BaseStatementHandler
基础Statement处理器
protected final Configuration configuration;
/**
* 对象工厂,用于创建返回值类型实例
*/
protected final ObjectFactory objectFactory;
protected final TypeHandlerRegistry typeHandlerRegistry;
/**
* 数据集处理器
*/
protected final ResultSetHandler resultSetHandler;
/**
* 参数处理器
*/
protected final ParameterHandler parameterHandler;
// 执行器
protected final Executor executor;
// 映射Statement
protected final MappedStatement mappedStatement;
// 分页参数
protected final RowBounds rowBounds;
// 动态sql
protected BoundSql boundSql;
protected BaseStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
this.configuration = mappedStatement.getConfiguration();
this.executor = executor;
this.mappedStatement = mappedStatement;
this