SqlSessionFactory是MyBatis框架的核心接口之一,它的主要作用是创建SqlSession对象,而SqlSession则是执行SQL语句的关键对象。下面我们来详细解析一下SqlSessionFactory的源码实现。
首先我们要了解的是SqlSessionFactory的接口定义,包括它的方法和返回值类型等:
public interface SqlSessionFactory {
/**
* Opens a new SqlSession.
* @return instance of SqlSession
*/
SqlSession openSession();
/**
* Opens a new SqlSession, specifying whether it is auto-committed or not.
* @param autoCommit Flag to indicate whether the session is auto-committed
* @return instance of SqlSession
*/
SqlSession openSession(boolean autoCommit);
/**
* Opens a new SqlSession with the specified executor type.
* @param execType Executor type to be used
* @return instance of SqlSession
*/
SqlSession openSession(ExecutorType execType);
/**
* Opens a new SqlSession with the specified executor type, and specifying whether it is auto-committed or not.
* @param execType Executor type to be used
* @param autoCommit Flag to indicate whether the session is auto-committed
* @return instance of SqlSession
*/
SqlSession openSession(ExecutorType execType, boolean autoCommit);
/**
* Gets the Configuration object used by this SqlSessionFactory.
* @return Configuration
*/
Configuration getConfiguration();
}
从上面的接口定义可以看出,SqlSessionFactory主要有以下几个方法:
- openSession() :打开一个新的SqlSession实例,事务默认非自动提交。
- openSession(boolean autoCommit) :打开一个新的SqlSession实例,可以根据autoCommit参数来设置事务是否自动提交。
- openSession(ExecutorType execType) :打开一个新的SqlSession实例,可以通过ExecutorType参数来指定执行器类型,事务默认非自动提交。
- openSession(ExecutorType execType, boolean autoCommit) :打开一个新的SqlSession实例,可以通过ExecutorType参数来指定执行器类型,可以根据autoCommit参数来设置事务是否自动提交。
- getConfiguration() :获取当前SqlSessionFactory的配置信息。
SqlSessionFactory的实现类是SqlSessionFactoryBuilder,下面我们来看一下SqlSessionFactoryBuilder的源码实现。
SqlSessionFactoryBuilder的构造函数源码如下:
public SqlSessionFactoryBuilder() {
this.configuration = new Configuration();
}
可以看出,SqlSessionFactoryBuilder的构造函数中创建了一个Configuration对象。
SqlSessionFactoryBuilder的build()方法源码如下:
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
return build(parser.parse());
} catch (Exception e) {
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);
}
可以看出,build()方法有两个重载版本,其中一个是读取XML文件构建SqlSessionFactory,另一个则是直接传入Configuration对象构建SqlSessionFactory。build()方法最终返回的是DefaultSqlSessionFactory对象。
DefaultSqlSessionFactory是SqlSessionFactory的默认实现类,它的构造函数如下:
public DefaultSqlSessionFactory(Configuration configuration) {
this.configuration = configuration;
}
可以看出,DefaultSqlSessionFactory的构造函数需要一个Configuration对象作为参数。
最后,我们再来看一下SqlSessionFactory的另一个实现类SqlSessionManager。SqlSessionManager也实现了SqlSessionFactory接口,不同的是它是线程安全的,这意味着多个线程可以共享同一个SqlSessionManager实例,并且可以通过它来获取SqlSession实例。SqlSessionManager的源码实现就不再赘述了,有兴趣的读者可以自行了解。