带你学习Mybatis之StatementHandler数据库交互

数据开
• 阅读 138

StatementHandler数据库交互

StatementHandler接口是mybatis的核心接口之一,StatementHandler接口中功能很多,进行预编译并且调用ParameterHandler的setParameters()方法设置参数,创建Statement对象,通过ParameterHandler为SQL语句绑定实参,执行select、insert、update、delete等多种类型的SQL语句,批量执行SQL语句,通过ResultSetHandler将结果集映射成结果对象。

StatementHandler流程

  • 创建: 由Configuration方法中newStatementHandler()方法生成的,生成的是RoutingStatementHandler
  • 选择: RoutingStatementHandler的通过适配器模式找到对应(根据上下文)的StatementHandler执行的,并且有SimpleStatementHandler、PrepareStatementHandler、CallableStatementHandler
  • Executor会调用StatementHandler的prepare方法创建Statement,调用parameterize方法通过ParameterHandler设置参数,完成预编译(只有Statement为PreparedStatement时才会进行预编译)
  • 最后调用ResultSetHandler封装结果集

<!-- more -->

public interface StatementHandler {
    // 从连接中获取一个Statement
  Statement prepare(Connection connection, Integer transactionTimeout)
      throws SQLException;
    // 绑定statement执行时所需的实参
  void parameterize(Statement statement)
      throws SQLException;
    // 批量执行SQL语句
  void batch(Statement statement)
      throws SQLException;
    // 执行update/insert/delete语句
  int update(Statement statement)
      throws SQLException;
    // 执行select语句
  <E> List<E> query(Statement statement, ResultHandler resultHandler)
      throws SQLException;

  <E> Cursor<E> queryCursor(Statement statement)
      throws SQLException;

  BoundSql getBoundSql();
    // 获取其中封装的ParameterHandler
  ParameterHandler getParameterHandler();
}

StatementHandler有两个实现类RoutingStatementHandler和BaseStatementHandler

BaseStatementHandler

BaseStatementHandler是实现了一个StatementHandler接口的抽象类,只提供了参数绑定相关的方法

public abstract class BaseStatementHandler implements StatementHandler {
    protected final Configuration configuration;
    protected final ObjectFactory objectFactory;
    protected final TypeHandlerRegistry typeHandlerRegistry;
      // 将结果集映射为对象
    protected final ResultSetHandler resultSetHandler;
      // 主要是为SQL语句绑定实参,使用传入的实参替换SQL语句的中?占位符
    protected final ParameterHandler parameterHandler;
      // 记录执行SQL语句的Executor对象
    protected final Executor executor;
      // 记录SQL语句对应的MappedStatement
    protected final MappedStatement mappedStatement;
      // 记录用户设置的offset和limit,用于用在结果集中定位映射的起始位置和结束位置
    protected final RowBounds rowBounds;
      // 记录SQK语句对应的BoundSql
    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.rowBounds = rowBounds;
        this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();
        this.objectFactory = this.configuration.getObjectFactory();
        if (boundSql == null) {
              // 调用keyGenerator的processBefore方法获取主键
            this.generateKeys(parameterObject);
            boundSql = mappedStatement.getBoundSql(parameterObject);
        }

        this.boundSql = boundSql;
        this.parameterHandler = this.configuration.newParameterHandler(mappedStatement, parameterObject, boundSql);
        this.resultSetHandler = this.configuration.newResultSetHandler(executor, mappedStatement, rowBounds, this.parameterHandler, resultHandler, boundSql);
    }

    public BoundSql getBoundSql() {
        return this.boundSql;
    }

    public ParameterHandler getParameterHandler() {
        return this.parameterHandler;
    }
        // 实现StatementHandler的prepare方法
    public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
        ErrorContext.instance().sql(this.boundSql.getSql());
        Statement statement = null;

        try {
              // 初始化Statement对象,由子类实现(模板方法模式)
            statement = this.instantiateStatement(connection);
            this.setStatementTimeout(statement, transactionTimeout);
            this.setFetchSize(statement);
            return statement;
        } catch (SQLException var5) {
            this.closeStatement(statement);
            throw var5;
        } catch (Exception var6) {
            this.closeStatement(statement);
            throw new ExecutorException("Error preparing statement.  Cause: " + var6, var6);
        }
    }

    protected abstract Statement instantiateStatement(Connection var1) throws SQLException;

    protected void setStatementTimeout(Statement stmt, Integer transactionTimeout) throws SQLException {
        Integer queryTimeout = null;
        if (this.mappedStatement.getTimeout() != null) {
            queryTimeout = this.mappedStatement.getTimeout();
        } else if (this.configuration.getDefaultStatementTimeout() != null) {
            queryTimeout = this.configuration.getDefaultStatementTimeout();
        }

        if (queryTimeout != null) {
            stmt.setQueryTimeout(queryTimeout);
        }

        StatementUtil.applyTransactionTimeout(stmt, queryTimeout, transactionTimeout);
    }

    protected void setFetchSize(Statement stmt) throws SQLException {
        Integer fetchSize = this.mappedStatement.getFetchSize();
        if (fetchSize != null) {
            stmt.setFetchSize(fetchSize);
        } else {
            Integer defaultFetchSize = this.configuration.getDefaultFetchSize();
            if (defaultFetchSize != null) {
                stmt.setFetchSize(defaultFetchSize);
            }

        }
    }

    protected void closeStatement(Statement statement) {
        try {
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException var3) {
        }

    }

    protected void generateKeys(Object parameter) {
        KeyGenerator keyGenerator = this.mappedStatement.getKeyGenerator();
        ErrorContext.instance().store();
        keyGenerator.processBefore(this.executor, this.mappedStatement, (Statement)null, parameter);
        ErrorContext.instance().recall();
    }
}

在BoundSql中记录的SQL语句中可能包含"?"占位符,而每个"?"占位符都对应了BoundSql.parameterMappings集合中的一个元素,ParameterMapping对象中记录了对应参数的名称以及相关属性
在ParameterHandler中的setParameters方法负责为SQL语句绑定实参,该接口只有一个实现类DefaultParameterHandler,遍历BoundSql.parameterMappings集合,根据ParameterMapping对象中记录的参数名称查找相应的实参,通过TypeHandler转换为JdbcType

SimpleStatementHandler

SimpleStatementHandler继承了BaseStatementHandler抽象类,底层直接使用java.sql.Statement对象来操作数据库,SQL中不能存在占位符,parameterize方法为空实现

public class SimpleStatementHandler extends BaseStatementHandler {
    public SimpleStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
        super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
    }
        
      //增删改都会走update方法
    public int update(Statement statement) throws SQLException {
        String sql = this.boundSql.getSql();
        Object parameterObject = this.boundSql.getParameterObject();
        KeyGenerator keyGenerator = this.mappedStatement.getKeyGenerator();
        int rows;
        if (keyGenerator instanceof Jdbc3KeyGenerator) {
            statement.execute(sql, 1);
              // 受影响的行数
            rows = statement.getUpdateCount();
              // 数据库生成的主键添加到parameterObject中
            keyGenerator.processAfter(this.executor, this.mappedStatement, statement, parameterObject);
        } else if (keyGenerator instanceof SelectKeyGenerator) {
            statement.execute(sql);
            rows = statement.getUpdateCount();
            keyGenerator.processAfter(this.executor, this.mappedStatement, statement, parameterObject);
        } else {
            statement.execute(sql);
            rows = statement.getUpdateCount();
        }

        return rows;
    }

    public void batch(Statement statement) throws SQLException {
        String sql = this.boundSql.getSql();
        statement.addBatch(sql);
    }

    public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
        String sql = this.boundSql.getSql();
        statement.execute(sql);
        return this.resultSetHandler.handleResultSets(statement);
    }

    public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
        String sql = this.boundSql.getSql();
        statement.execute(sql);
        return this.resultSetHandler.handleCursorResultSets(statement);
    }
        
      // 初始化Statement
    protected Statement instantiateStatement(Connection connection) throws SQLException {
          // 直接使用Connection对象创建Statement对象
        return this.mappedStatement.getResultSetType() == ResultSetType.DEFAULT ? connection.createStatement() : connection.createStatement(this.mappedStatement.getResultSetType().getValue(), 1007);
    }

    public void parameterize(Statement statement) {
    }
}

PreparedStatementHandler

PreparedStatementHandler继承了BaseStatementHandler抽象类,底层使用java.sql.PreparedStatement对象来操作数据库,PreparedStatement是预编译的,可以在sql语句中存在"?"占位符,parameterize方法通过调用ParameterHandler.setParameters()方法完成SQL语句的参数绑定

public class PreparedStatementHandler extends BaseStatementHandler {
    public PreparedStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
        super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
    }

    public int update(Statement statement) throws SQLException {
        PreparedStatement ps = (PreparedStatement)statement;
        ps.execute();
        int rows = ps.getUpdateCount();
        Object parameterObject = this.boundSql.getParameterObject();
        KeyGenerator keyGenerator = this.mappedStatement.getKeyGenerator();
        keyGenerator.processAfter(this.executor, this.mappedStatement, ps, parameterObject);
        return rows;
    }

    public void batch(Statement statement) throws SQLException {
        PreparedStatement ps = (PreparedStatement)statement;
        ps.addBatch();
    }

    public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
        PreparedStatement ps = (PreparedStatement)statement;
        ps.execute();
        return this.resultSetHandler.handleResultSets(ps);
    }

    public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
        PreparedStatement ps = (PreparedStatement)statement;
        ps.execute();
        return this.resultSetHandler.handleCursorResultSets(ps);
    }
        
      //初始化Statement,进行预编译
    protected Statement instantiateStatement(Connection connection) throws SQLException {
        String sql = this.boundSql.getSql();
          // 根据mappedStatement.getKeyGenerator()字段的值,创建PrepareStatement
        if (this.mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {
            String[] keyColumnNames = this.mappedStatement.getKeyColumns();
            return keyColumnNames == null ? connection.prepareStatement(sql, 1) : connection.prepareStatement(sql, keyColumnNames);
        } else {
            return this.mappedStatement.getResultSetType() == ResultSetType.DEFAULT ? connection.prepareStatement(sql) : connection.prepareStatement(sql, this.mappedStatement.getResultSetType().getValue(), 1007);
        }
    }

    public void parameterize(Statement statement) throws SQLException {
        this.parameterHandler.setParameters((PreparedStatement)statement);
    }
}

CallableStatementHandler

CallableStatementHandler继承了BaseStatementHandler抽象类,底层使用java.sql.CallableStatement调用指定的存储过程,其parameterize方法通过调用ParameterHandler.setParameters()方法完成SQL语句的参数绑定,并指定输出参数的索引位置和JDBC类型。

public class CallableStatementHandler extends BaseStatementHandler {
    public CallableStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
        super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
    }

    public int update(Statement statement) throws SQLException {
        CallableStatement cs = (CallableStatement)statement;
        cs.execute();
        int rows = cs.getUpdateCount();
        Object parameterObject = this.boundSql.getParameterObject();
        KeyGenerator keyGenerator = this.mappedStatement.getKeyGenerator();
        keyGenerator.processAfter(this.executor, this.mappedStatement, cs, parameterObject);
        this.resultSetHandler.handleOutputParameters(cs);
        return rows;
    }

    public void batch(Statement statement) throws SQLException {
        CallableStatement cs = (CallableStatement)statement;
        cs.addBatch();
    }

    public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
        CallableStatement cs = (CallableStatement)statement;
        cs.execute();
        List<E> resultList = this.resultSetHandler.handleResultSets(cs);
        this.resultSetHandler.handleOutputParameters(cs);
        return resultList;
    }

    public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
        CallableStatement cs = (CallableStatement)statement;
        cs.execute();
        Cursor<E> resultList = this.resultSetHandler.handleCursorResultSets(cs);
        this.resultSetHandler.handleOutputParameters(cs);
        return resultList;
    }

    protected Statement instantiateStatement(Connection connection) throws SQLException {
        String sql = this.boundSql.getSql();
        return this.mappedStatement.getResultSetType() == ResultSetType.DEFAULT ? connection.prepareCall(sql) : connection.prepareCall(sql, this.mappedStatement.getResultSetType().getValue(), 1007);
    }

    public void parameterize(Statement statement) throws SQLException {
        this.registerOutputParameters((CallableStatement)statement);
        this.parameterHandler.setParameters((CallableStatement)statement);
    }

    private void registerOutputParameters(CallableStatement cs) throws SQLException {
        List<ParameterMapping> parameterMappings = this.boundSql.getParameterMappings();
        int i = 0;

        for(int n = parameterMappings.size(); i < n; ++i) {
            ParameterMapping parameterMapping = (ParameterMapping)parameterMappings.get(i);
            if (parameterMapping.getMode() == ParameterMode.OUT || parameterMapping.getMode() == ParameterMode.INOUT) {
                if (null == parameterMapping.getJdbcType()) {
                    throw new ExecutorException("The JDBC Type must be specified for output parameter.  Parameter: " + parameterMapping.getProperty());
                }

                if (parameterMapping.getNumericScale() != null && (parameterMapping.getJdbcType() == JdbcType.NUMERIC || parameterMapping.getJdbcType() == JdbcType.DECIMAL)) {
                    cs.registerOutParameter(i + 1, parameterMapping.getJdbcType().TYPE_CODE, parameterMapping.getNumericScale());
                } else if (parameterMapping.getJdbcTypeName() == null) {
                    cs.registerOutParameter(i + 1, parameterMapping.getJdbcType().TYPE_CODE);
                } else {
                    cs.registerOutParameter(i + 1, parameterMapping.getJdbcType().TYPE_CODE, parameterMapping.getJdbcTypeName());
                }
            }
        }

    }
}

RoutingStatementHandler

RoutingStatementHandler是StatementHandler接口的另一个实现类,会根据MappedStatement中指定的statementType来创建对应的StatementHandler接口实现。

public class RoutingStatementHandler implements StatementHandler {
    private final StatementHandler delegate;

    public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
          // 根据statementType来设置相应的接口实现
        switch(ms.getStatementType()) {
        case STATEMENT:
            this.delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
            break;
        case PREPARED:
            this.delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
            break;
        case CALLABLE:
            this.delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
            break;
        default:
            throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
        }

    }

    public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
        return this.delegate.prepare(connection, transactionTimeout);
    }

    public void parameterize(Statement statement) throws SQLException {
        this.delegate.parameterize(statement);
    }

    public void batch(Statement statement) throws SQLException {
        this.delegate.batch(statement);
    }

    public int update(Statement statement) throws SQLException {
        return this.delegate.update(statement);
    }

    public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
        return this.delegate.query(statement, resultHandler);
    }

    public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
        return this.delegate.queryCursor(statement);
    }

    public BoundSql getBoundSql() {
        return this.delegate.getBoundSql();
    }

    public ParameterHandler getParameterHandler() {
        return this.delegate.getParameterHandler();
    }
}
https://zhhll.icu/2020/框架/mybatis/组件分析/5.mybatis之StatementHandler/

本文由mdnice多平台发布

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
3年前
java中的方法返回值使用泛型,实现灵活的返回值类型
<divclass"htmledit\_views"<p<br</p<p痛点:&nbsp;&nbsp;&nbsp;使用Mybatis框架的时候,想封装一个底层JDBC控制器,用于提供和Mybatis交互的增删改查接口(公用的接口),但由于公用的查询方法可能是用户自定义的任意一个和表对应的javabean类型,所以要考虑怎么样给调用
Stella981 Stella981
3年前
SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器!复制代码(https://oscimg.oschina.net/oscnet/675f5689159acfa2c39c91f4df40a00ce0f.gif)/cookie对象;rememberMeCookie()方法是设置Cookie的生成模
Stella981 Stella981
3年前
MyBatis 面试题(附答案解析)
目录MyBatis的实现逻辑MyBatis的缓存实现逻辑{}和${}的区别是什么?MyBatis中自定义标签的执行原理简述Mapper接口的工作原理在Spring中Mapper接口是如何被注入的?在Mapper接口中是否可以有重载方法?当实体类中的属性名和表中的字
Stella981 Stella981
3年前
MyBatis插件开发
MyBatis插件开发一、前言MyBatis在四大对象的创建过程中,都会有插件进行介入。插件可以利用动态代理机制一层层的包装目标对象,而实现在目标对象执行目标方法之前进行拦截的效果。MyBatis允许在已映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis允许使用插件来拦截的方法调用包
Stella981 Stella981
3年前
SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件
一、Mybatis框架1、mybatis简介MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解来配置和映射原生类型、接口和Java的POJO
Easter79 Easter79
3年前
SpringBoot2.0 基础案例(10):整合Mybatis框架,集成分页助手插件
一、Mybatis框架1、mybatis简介MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解来配置和映射原生类型、接口和Java的POJO
Easter79 Easter79
3年前
SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器!复制代码(https://oscimg.oschina.net/oscnet/675f5689159acfa2c39c91f4df40a00ce0f.gif)/cookie对象;rememberMeCookie()方法是设置Cookie的生成模
Stella981 Stella981
3年前
Mybateis mapper 接口 example 用法
注意:希望通过此篇文章分享可以使大家对mapper接口以及example用法更加深入理解MyBatis的Mapper接口以及Example的实例函数及详解一、mapper接口中的
Stella981 Stella981
3年前
Mybatis学习——Mybatis核心配置
MyBatis的核心配置在使用MyBatis框架时,设计两个核心的d对象:SqlSessionFactory和SqlSession.SqlsessionFactorySqlSessionFactory是单个数据库映射关系经过编译后的内存镜像,其主要作用用来创建SqlSession对象,SqlS
Wesley13 Wesley13
3年前
1、原生jdbc连接oracle数据库简单介绍
一。jbdc的常用API1.Connection:数据库的链接对象2.statement:数据库sql执行对象3.preparedStatment:sql的预编译处理对象,是statement子接口4.resultset:返回查询的结果集二。jdbc开发步骤1.在项目中加入驱动jar包2.写jdbc链接
Wesley13 Wesley13
3年前
Java 工厂方法模式
在工厂对象上调用创建方法,生成接口的某个实现的对象通过这种方式,接口与实现分离方法接口/方法接口/publicinterfaceService{voidmethod1();voidmethod2();}