mybatis中获取sqlSession的源码分析

本文详细介绍了MyBatis框架中的关键组件SqlSessionFactory、DefaultSqlSessionFactory及DefaultSqlSession的工作原理。SqlSessionFactory负责创建SqlSession实例,而DefaultSqlSessionFactory通过配置信息和事务管理策略生成执行器,进而创建DefaultSqlSession用于执行SQL语句。

 

 

0 SqlSessionFactoryBuilder类

SqlSessionFactoryBuilder sqlSessionFacotory=SqlSessionFactoryBuilder().build(reader)

public SqlSessionFactory build(Reader reader) {

return build(reader, null, null);

}

 

它使用了一个参照了XML文档或更特定的SqlMapConfig.xml文件的Reader实例。

//可选的参数是environment和properties。Environment决定加载哪种环境(开发环境/生产环境),包括数据源和事务管理器。

//如果使用properties,那么就会加载那些properties(属性配置文件),那些属性可以用${propName}语法形式多次用在配置文件中。和Spring很像,一个思想?

public SqlSessionFactory build(Reader reader, String environment, Properties properties) {

try {

//委托XMLConfigBuilder来解析xml文件,并构建

XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);

return build(parser.parse());

} catch (Exception e) {

//这里是捕获异常,包装成自己的异常并抛出的idiom?,最后还要reset ErrorContext

throw ExceptionFactory.wrapException("Error building SqlSession.", e);

} finally {

ErrorContext.instance().reset();

try {

reader.close();

} catch (IOException e) {

// Intentionally ignore. Prefer previous error.

}

}

}

 

public SqlSessionFactory build(Configuration config) {

return new DefaultSqlSessionFactory(config);

}

1 DefaultSqlSessionFactory类

@Override

public SqlSession openSession() {

return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);

}

 

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

//然后产生一个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();

}

}

 

2.DefaultSqlSession类

public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {

this.configuration = configuration;

this.executor = executor;

this.dirty = false;

this.autoCommit = autoCommit;

}

 

获得DefaultSqlSession类其中包含执行器(事务相关),配置信息

public interface SqlSession extends Closeable {.....}

SqlSession session = sqlMapper.openSession();

转载于:https://my.oschina.net/iioschina/blog/1860183

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值