类列表:
SqlSessionFactory 说明:sqlsession对象的工场用于创建sqlsession。
SqlSession 说明:执行sql的接口类。
Configuration 说明:数据库连接和sql语句配置类。
MappedStatement 说明:数据库操作的一些变量
Executor 说明数据操作方法接口类
步骤:
加载xml文件到XMLConfigBuilder,生成DefaultSqlSessionFactory ,再生成DefaultSqlSession。
执行DefaultSqlSessionFactory.openSessionFromDataSource获取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, autoCommit);
return new DefaultSqlSession(configuration, executor);
} 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();
}
}