博客
关于我
2021-04-11 MyBatis运行流程-02
阅读量:169 次
发布时间:2019-02-28

本文共 3048 字,大约阅读时间需要 10 分钟。

文章目录

获取SqlSession对象

具体操作

  1. openSession()
  2. openSessionFromDataSource()
  3. 获取一些信息,创建事物管理器tx
  4. newExecutor()
  5. 根据Executor在全局配置中的类型,创建出SimpleExecutor/ReuseExecutor/BatchExecutor
  6. 如果有二级缓存配置开启,创建CachingExecutor(executor)
  7. 使用每一个拦截器重新包装executor并返回
  8. 创建DefaultSqlSession,包含ConfigurationExecutor
  9. 返回DefaultSqlSession

源码分析

1. openSession()

SqlSession openSession = sqlSessionFactory.openSession();

2. openSessionFromDataSource()

configuration中拿到默认的Executor类型。Executor类型的类型有三种:SIMPLE/REUSE/BATCH

@Override  public SqlSession openSession() {       return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);  }

3. 获取一些信息,创建事物管理器tx

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);      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();    }  }

4. newExecutor()

传入的参数为事务管理器和Executor的类型,根据传入的类型来创建ExecutorExecutor是一个接口,是用来增删改查的。

final Executor executor = configuration.newExecutor(tx, execType);

5. 根据Executor在全局配置中的类型,创建出SimpleExecutor/ReuseExecutor/BatchExecutor

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包装    if (cacheEnabled) {         executor = new CachingExecutor(executor);    }    //使用拦截器包装executor    executor = (Executor) interceptorChain.pluginAll(executor);    return executor;  }

6. 如果有二级缓存配置开启,创建CachingExecutor(executor)

对二级缓存的具体操作:

public CachingExecutor(Executor delegate) {       this.delegate = delegate;    delegate.setExecutorWrapper(this);  }

7. 使用每一个拦截器重新包装executor并返回

pluginAll具体实现:拿到所有的拦截器,调用每一个拦截器的plugin方法。

public Object pluginAll(Object target) {       for (Interceptor interceptor : interceptors) {         target = interceptor.plugin(target);    }    return target;  }

8.创建DefaultSqlSession,包含ConfigurationExecutor

在这里插入图片描述

9.返回DefaultSqlSession

总结:

DefaultSqlSessionsqlSession的实现类,里面包含了ExecutorConfigurationExecutor在这一步会被创建。

转载地址:http://mpwc.baihongyu.com/

你可能感兴趣的文章
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>