mybatis原理解析---SqlSession运行过程(下)

本文深入解析Mybatis SqlSession的运行过程,包括执行器Executor、数据库会话器StatementHandler、参数处理器ParameterHandler和结果处理器ResultSetHandler的作用及相互配合。详细介绍了各组件在SQL执行和结果处理中的职责,揭示了SqlSession执行查询和更新的基本逻辑。
摘要由CSDN通过智能技术生成

继续sqlSession运行过程分析,上一篇文章讲到通过sqlSession中executor对象的query方法执行的查询,在分析这个方法的源码之前,先分析下SqlSession执行sql中用到的比较重要的几个对象。SqlSession中是通过Executor 、StatementHandler、ParameterHandler、ResultHandler来完成数据库操作和结果返回的。先稍微来了解下这几个对象大致的作用:

  • Executor:代表执行器,由其来调度StatementHandler、ParameterHandler、ResultHandler来执行对应的sql并返回结果。
  • StatementHandler:使用数据库的Statement对象(PrepareStatement)完成实际的查询,是四大对象的核心,起到承上启下的作用。
  • ParameterHandler:用来sql对参数的处理。
  • ResultHandler:对查询返回的结果集(ResultSet)进行封装处理后返回。

下面详细来研究上面提到的四个对象。

1.执行器(Executor)

执行器起(Executor)了至关重要的作用。它是一个真正执行Java和数据库交互的组件,在Mybatis中存在三种类型的执行器,可以在setting属性中配置:

  • SIMPLE,简单执行器,不配置它就是默认的执行器。
  • REUSE,一种重用预处理语句的执行器。
  • BATCH,执行会重用预处理语句和支持批量更新,它是针对批量操作专用的执行器。
    他们都提供了查询和更新相关的方法以及相关事务的方法,下面是Executor接口中的部分方法:
 int update(MappedStatement ms, Object parameter) throws SQLException;

   <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;

   <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException;

   <E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException;

   List<BatchResult> flushStatements() throws SQLException;

   void commit(boolean required) throws SQLException;

   void rollback(boolean required) throws SQLException;

下面先不看这些方法的具体实现,而是先来了解下如何来创建Executor,首先sqlSession中关联的Executor对象是在调用SqlSessionFactory.openSession()的时候创建的,具体的代码如下:

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      //execType是执行器的类型,在openSession()方法中可以传入; tx是指定的事务类型
      final Executor executor = configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
      closeTransaction(tx); // may have fetched a connection so lets call close()
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

可以看到Executor对象是通过configuration.newExecutor()方法创建的,传入的两个参数是事务类型和传入的执行器类型;下面来看下这个方法的具体实现:

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);
    }
    //如果配置了二级缓存,则会创建一个CachingExecutor对当前Executor进行一层包装,这个对象负责执行缓存逻辑
    if (cacheEnabled) 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值