Mybatis 源码之获取映射器类

获取 SqlSession 对象

定位: org.apache.ibatis.session.SqlSessionFactory#openSession
作用: 创建 SqlSession 实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
Transaction tx = null;
try {
// 获取 mybatis-config.xml 配置文件中配置的 Environment 对象
final Environment environment = configuration.getEnvironment();
// 获取 TransactionFactory 对象
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
// 创建 Transaction 对象
tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
// 根据配置创建 Executor 对象
final Executor executor = configuration.newExecutor(tx, execType);
// 然后产生一个 DefaultSqlSession
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();
}
}
private SqlSession openSessionFromConnection(ExecutorType execType, Connection connection) {
try {
boolean autoCommit;
try {
// 获取当前连接的事务是否为自动提交方法
autoCommit = connection.getAutoCommit();
} catch (SQLException e) {
// Failover to true, as most poor drivers
// or databases won't support transactions
// 当前数据库驱动提供的连接不支持事务,则可能会抛出异常
autoCommit = true;
}
// 获取 mybatis-config.xml 配置文件中配置的 Environment 对象
final Environment environment = configuration.getEnvironment();
// 获取 TransactionFactory 对象
final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
// 创建 Transaction 对象
final Transaction tx = transactionFactory.newTransaction(connection);
// 根据配置创建 Executor 对象
final Executor executor = configuration.newExecutor(tx, execType);
return new DefaultSqlSession(configuration, executor, autoCommit);
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}

创建执行器

定位: org.apache.ibatis.session.Configuration#newExecutor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 产生执行器
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
executorType = executorType == null ? defaultExecutorType : executorType;
executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
Executor 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 = (Executor) interceptorChain.pluginAll(executor);
return executor;
}

DefaultSqlSession 对象

DefaultSqlSession 实现了 SqlSession 接口的所有方法,包括对数据库的增删改查、事务管理等功能。