MyBatis源码分析(二)prepareStatement预编译的执行流程

通常我们如果自己写建立数据库连接的代码的时候,都会这么写

pstmt = conn.prepareStatement(sql);
pstmt.setString(1, email);
result = pstmt.executeQuery();

而Mybatis是怎么封装,又是怎么进行预编译的呢,今天就一文让你理解Mybatis的原理

一、获取Executor

我们之前一篇文章《MyBatis源码分析(一)基本请求流程》讲了Mybatis执行的基本流程,包括Plugin插件。
其实在执行过程中,所有的sql语句都要经过执行器Executor。执行器创建的源码如下:

  public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    if (ExecutorType.BATCH == executorType) {
      executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
      executor = new ReuseExecutor(this, transaction);
    } else {
      executor = new SimpleExecutor(this, transaction);
    }
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

可以看到创建的执行器有三种,分别是

  • BatchExecutor:批量执行的执行器,官方说明这个executor是用于执行存储过程的和批量操作的,因此这个方法是循环或者多次执行构建一个存储过程或批处理过程。BatchExecutor 的事务是没法自动提交的。因为 BatchExecutor 只有在调用了 SqlSession 的 commit 方法的时候 , 它才会去执行 executeBatch 方法。这个执行器基本用不到,现在的正常的公司都不允许搞存储过程。
  • ReuseExecutor:会缓存创建的prepareStatement,但是只能在同一个sqlSession中,用处不大,待会说prepareStatement还会再介绍这个执行器。
  • SimpleExecutor:是默认的执行器,这个执行器是在什么地方指定的呢,在Configuration类中:
protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;

以上的三个Executor都有同一个父类:BaseExecutor,在BaseExecutor的query方法中,调用了三个子类的doQuery方法,update方法中,调用了三个子类的doUpdate方法,这是用了模板方法设计模式

二、SqlSession

SqlSession是一次数据库连接,在Mybatis中,是通过如下方式创建的:
SqlSession创建
同时也可以看到,一个SqlSession对应创建一个新的执行器Executor。这也是为什么我们上面说ReuseExecutor没有什么用处的原因,在Mybatis中一个sql语句就会创建一个SqlSession,创建一个执行器Executor,ReuseExecutor就算缓存了PrepareStatement也用不上。

三、关闭连接

在执行sql的时候,会调用到SqlSessionTemplate,这是一个模板方法的类。在invoke方法执行完后,会调用finally方法关闭连接:

   } finally {
       if (sqlSession != null) {
           SqlSessionUtils.closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
       }

   }

这个方法中调用了session.close(),里面又调用了executor.close()方法去关闭SqlSession,Executor从而关闭SqlSession,释放连接。

四、SimpleExecutor

SimpleExecutor是Mybatis默认使用的执行器,其doQuery方法如下:

  @Override
  public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    Statement stmt = null;
    try {
      Configuration configuration = ms.getConfiguration();
      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
      stmt = prepareStatement(handler, ms.getStatementLog());
      return handler.<E>query(stmt, resultHandler);
    } finally {
      closeStatement(stmt);
    }
  }

可以看到里面限制性的prepareStatement方法,然后又执行了query方法。
prepareStatement方法里面其实就是封装了我们手动创建PrepareStatement的时候的conn.prepareStatement方法,先去进行预编译,预编译完成后,在调用query方法执行。
由此可见Mybatis底层还是用的我们自己创建Statement并执行sql语句的方法。

五、SqlNode

在Mybatis中,我们都要写xml文件,例如这样一段xml写的sql:

    <select id="selectByIdList" resultMap="BaseResultMap" parameterType="Long" >
        select
        <include refid="Base_Column_List" />
        from table_1
        where id in
        <foreach collection="list" item="status_id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

Mybatis是怎么把这样的语句解析成sql语句的呢,就是通过SqlNode,SqlNode是一个接口类,它有很多子类
SqlNode子类
例如IfSqlNode就是用来解析Mybatis文件下面的 <if 标签,其入口就是在MappedStatement.getBoundSql方法中,经过一系列SqlNode解析后,就变成了一条正常的sql语句。

六、总结

本篇文章主要向你介绍了执行sql的一些关键节点:

  • Executor:执行有有BatchExecutor、SimpleExecutor、ReuseExecutor,这三个执行器默认的同时也是最常用的是SimpleExecutor
  • SqlSession: 在Mybatis中正常情况下执行sql的会话,sql执行完则关闭此会话,同时还会关闭对应的执行器。
  • SqlNode:在Mybatis中的xml语法,怎么解析成Sql语句呢?就是通过SqlNode

在读了这篇文章后,你是不是对Mybatis的原理理解地非常深入了呢?为了让你更好的进行开发工作,博主创建了一篇专栏教你精通使用在工作过程中的各种工具:《精通java开发工具》快来提升你的工作效率吧。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值