mybatis-StatementHandler解析

本文深入探讨了MyBatis的StatementHandler接口及其实现,包括BaseStatementHandler的通用逻辑,SimpleStatementHandler、PreparedStatementHandler和CallableStatementHandler的特有功能。总结了StatementHandler在JDBC操作中的角色,如何处理参数、执行SQL以及结果转换。
摘要由CSDN通过智能技术生成

概述

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值