Java源码-SqlSessionFactory源码解析

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的源码实现就不再赘述了,有兴趣的读者可以自行了解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值