mybatis源码分析三之SqlSession

SqlSessionSqlSession下的四大对象ExecutorStatementHandlerparamsHandlerresultSetHandlerSqlSessionSqlSession是一个会话,相当于jdbc的Connection对象,生命周期应该是请求数据库处理事务的过程中。它是非线程安全的,在每次创建SqlSession都必须及时关闭它,它...
摘要由CSDN通过智能技术生成

SqlSession

  1. SqlSession是一个会话,相当于jdbc的Connection对象,生命周期应该是请求数据库处理事务的过程中。它是非线程安全的,在每次创建SqlSession都必须及时关闭它,它长期存在就会使数据库连接池的活动资源减少。

SqlSession下的四大对象

  1. Mapper执行的过程是通过Executor、StatementHandler、ParameterHandler和ResultHandler来完成数据库操作和结果返回的
    • Executor表示执行器,调度相关的handler来执行对应的sql(StatementHandler、ParameterHandler、ResultHandler)
    • StatementHandler的作用是使用数据库的Statement(PreparedStatement)执行操作
    • ParameterHandler用于SQL对参数的处理
    • ResultHandler是进行最后数据集(ResultSet)的封装返回处理的

Executor

  1. Executor是一个真正执行java和数据库交互的东西
    • SIMPLE
    • REUSE
    • BATCH
  2. 创建

    在DefaultSqlSessionFactory中创建DefaultSqlSession之前创建Executor(通过Configuration创建),以下是Configuration中创建Executor的代码。根据配置类型去创建三种执行器中的一种,在创建之后将Executor放到interceptorChain之中
     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;
      }
    
  3. SimpleExecutor

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值