您的位置:首页 > 数据库

SqlSession最常用的类

2013-05-11 00:00 267 查看
http://chengjisihan.iteye.com/blog/752168(SqlSession,SqlSessionFactory,SqlSessionFactoryBuilder,Environment,Configuration,Mapper)

http://chengjisihan.iteye.com/blog/755104(Interceptor,TypeHandler,ObjectFactory,TypeAlias,SelectBuilder)

http://chengjisihan.iteye.com/blog/760259(Dynamic SQL)

SqlSession中的几个方法在Dao中经常要用到。。
SqlSessionFactory是用来构造SqlSession的

public interface SqlSessionFactory {

SqlSession openSession();

SqlSession openSession(boolean autoCommit);
SqlSession openSession(Connection connection);
SqlSession openSession(TransactionIsolationLevel level);

SqlSession openSession(ExecutorType execType);
SqlSession openSession(ExecutorType execType, boolean autoCommit);
SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level);
SqlSession openSession(ExecutorType execType, Connection connection);

Configuration getConfiguration();

}
默认实现是DefaultSqlSessionFactory,还有一个是SqlSessonManager(身份不明)

public class DefaultSqlSessionFactory implements SqlSessionFactory {

private final Configuration configuration;

public DefaultSqlSessionFactory(Configuration configuration) {
this.configuration = configuration;
}
//此处省去很多openSession(...)
public Configuration getConfiguration() {
return configuration;
}
//这个才是核心
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();
}
}

从这个方法中可以看到构建顺序
Configuration --> Environment --> TransactionFactory --> Transaction -> Executor --> DefaultSqlSession
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: