Mybatis源码解析 StatementHandler

StatementHandler用于执行Statement相关操作

相关处理接口:

//实例化Statement
Statement prepare(Connection connection)
      throws SQLException;

//设置sql参数 
void parameterize(Statement statement)
      throws SQLException;

//增加批量语句但不执行,批量会在flush的时候统一执行
void batch(Statement statement)
      throws SQLException;

//执行更新语句(update/insert/delete)
int update(Statement statement)
      throws SQLException;

//执行query语句
 List query(Statement statement, ResultHandler resultHandler)
      throws SQLException;

//
BoundSql getBoundSql();

ParameterHandler getParameterHandler();

Mybatis默认实现:

Mybatis源码解析 StatementHandler_第1张图片
image.png

RoutingStatementHandler

委派模式,根据StatementType委派给图中其他3种handler中的一种

BaseStatementHandler

3种handle的基类,实现公共方法,并暴露抽象实现instantiateStatement给子类

SimpleStatementHandler

对应了Statement,用于执行简单的Sql语句

CallableStatementHandler

对应CallableStatement
用于执行存储过程

PreparedStatementHandler

对应PreparedStatement
执行带多项参数的SQL
eg:

分别对应了Statement,CallableStatement,PreparedStatement
Statement:执行简单sql语句
PreparedStatement:执行带参数的Sql
eg:

PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES
                                      SET SALARY = ? WHERE ID = ?");
pstmt.setBigDecimal(1, 153833.00)
pstmt.setInt(2, 110592)
pstmt.execute()

Mybatis默认的执行方式是prepare

mappedStatement.statementType = StatementType.PREPARED;

你可能感兴趣的:(Mybatis源码解析 StatementHandler)