MyBatis核心对象之StatementHandler
MyBatis核心对象之StatementHandler
StatementHandler
ResultHandler
ParameterHandler
Executor
org.apache.ibatis.executor.statement.StatementHandler
org.apache.ibatis.session.ResultHandler
org.apache.ibatis.executor.parameter.ParameterHandler
org.apache.ibatis.executor.Executor
public interface StatementHandler {
//获取语句
Statement prepare(Connection connection, Integer transactionTimeout)
throws SQLException;
//设置参数
void parameterize(Statement 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
BoundSql getBoundSql();
//获取参数处理器
ParameterHandler getParameterHandler();
}
public class RoutingStatementHandler implements StatementHandler {
private final StatementHandler delegate;
//核心,根据ms的语句类型来创建相应的StatementHandler
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());
}
}
@Override
public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
return delegate.prepare(connection, transactionTimeout);
}
@Override
public void parameterize(Statement statement) throws SQLException {
delegate.parameterize(statement);
}
@Override
public void batch(Statement statement) throws SQLException {
delegate.batch(statement);
}
@Override
public int update(Statement statement) throws SQLException {
return delegate.update(statement);
}
@Override
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
return delegate.query(statement, resultHandler);
}
@Override
public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
return delegate.queryCursor(statement);
}
@Override
public BoundSql getBoundSql() {
return delegate.getBoundSql();
}
@Override
public ParameterHandler getParameterHandler() {
return delegate.getParameterHandler();
}
}
@startuml
interface StatementHandler
class RoutingStatementHandler
StatementHandler <|.. RoutingStatementHandler
abstract class BaseStatementHandler
class PreparedStatementHandler
class CallableStatementHandler
class SimpleStatementHandler
StatementHandler <|.. BaseStatementHandler
BaseStatementHandler <|-- PreparedStatementHandler
BaseStatementHandler <|-- CallableStatementHandler
BaseStatementHandler <|-- SimpleStatementHandler
@enduml