SqlSession是如何创建的
创建SqlSession
先来张图,先大致感受下SqlSession组成部分:

SqlSession创建步骤:
- 解析MyBatis主配置文件mybatis-config.xml主要是datasource相关章节的配置
<environments default="development">
<environment id="development"> //对应类 Environment
<transactionManager type="JDBC"/> //对应类TransactionFactory创建不同事务管理器
<dataSource type="POOLED"> // 对应DataSourceFactory根据不同类型创建不同数据源
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
- 解析事务管理 transactionManager 节点,在mybatis中提供了JDBC事务和ManagedTransaction,JDBC事务则是直接调用Connection对像的commit()和rollback()对事务进行操作,而ManagedTransaction则不会对事务操作操作,虽然实现了Transaction接口,但commit()和rollback()方法是空实现。在此会创建TransactionFactory实现类JdbcTransaction。
- dataSource type=“POOLED”,在MyBatis中提供了简单的数据库连接池实现,一般在生产使用时会使用Druid连接池对连接进行管理。(后续会对Druid连接池源码进行讲解)在些会创建DataSource的实现类PooledDataSource。
- PooledDataSource最终还是会使用接UnpooledDataSource来创建数据源。
- UnpooledDataSource类实现了DataSource接口,使用配置的基本信息。如用户名、密码,驱动等。此类最主要的方法是doGetConnection()方法,根据读取的数据库配置信息使用DriverManager.getConnection来创建连接。
根据上面几步的创建最终会把txFactory,dataSource 用来构建Environment对像,
把构建好Environment对像设置到configuration对像中,以便整个mybatis中使用。
Environment.Builder environmentBuilder = new Environment.Builder(id)
.transactionFactory(txFactory)
.dataSource(dataSource);
configuration.setEnvironment(environmentBuilder.build());
使用SqlSession
上面已创建好了SqlSession对像,接下来看如何使用SqlSession对像,核心代码如下:
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();
}
}
- 调用用SqlSessionFactory的openSession方法
- openSession方法调用openSessionFromDataSource方法,这个方法会获取上步中放置到configuration对像中的Environment对像,从此对像中获取到TransactionFactory。
- 使用TransactionFactory事务工厂和从environment中获取到的dataSource 实例创建一个新的事务。
- 根据配置的Executor创建相应的运行器,默认是SimpleExecutor。Executor用来执行 SQL。Executor又要指定事务类型和执行器的类型。
- 创建DefaultSqlSession并返回。
Executor
Executor是执行sql语句的组件,如下是Executor类图

Executor我们看下主要的几个方
- int update(MappedStatement ms, Object parameter) throws SQLException;
参数说明:
- MappedStatement ms 在Mpper.xml中每一个方法对应一个MappedStatement 对像,即SQL映射语句。
- Object parameter 参数
- List query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException;
· - CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql);
本文详细介绍了SqlSession的创建步骤,包括解析配置文件、创建TransactionFactory、DataSource及Environment,最后展示如何通过SqlSessionFactory获取并使用SqlSession。重点涉及DataSource的选择和事务管理策略。
314

被折叠的 条评论
为什么被折叠?



