美文网首页
mybatis源码|SessionFactory机制原理

mybatis源码|SessionFactory机制原理

作者: ajajaj | 来源:发表于2020-07-22 22:28 被阅读0次

SqlSessionFactory是MyBatis的关键对象,它是个单个数据库映射关系经过编译后的内存镜像.SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象类获得,SqlSessionFactoryBuilder则可以从XML配置文件或一个预先定制的Configuration的实例构建出SqlSessionFactory的实例.每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心.同时SqlSessionFactory也是线程安全的,SqlSessionFactory一旦被创建,应该在应用执行期间都存在.在应用运行期间不要重复创建多次,建议使用单例模式.SqlSessionFactory是创建SqlSession的工厂.

mybatis的初始化流程

mybatis初始化流程如下图所示:

image
  • 1、调用 SqlSessionFactoryBuilder 对象的 build(inputStream) 方法;
  • 2、SqlSessionFactoryBuilder 会根据输入流 inputStream 等信息创建XMLConfigBuilder 对象 ;
  • 3、SqlSessionFactoryBuilder 调用 XMLConfigBuilder 对象的 parse() 方法;
  • 4、XMLConfigBuilder 对象返回 Configuration 对象;
  • 5、SqlSessionFactoryBuilder 根据 Configuration 对象创建一个DefaultSessionFactory 对象;
  • 6、SqlSessionFactoryBuilder 返回 DefaultSessionFactory 对象给 Client ,供 Client使用。
public class SqlSessionFactoryBuilder {

  public SqlSessionFactory build(Reader reader) {
    return build(reader, null, null);
  }

  public SqlSessionFactory build(Reader reader, String environment) {
    return build(reader, environment, null);
  }

  public SqlSessionFactory build(Reader reader, Properties properties) {
    return build(reader, null, properties);
  }

  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.
      }
    }
  }
 // 1、调用 SqlSessionFactoryBuilder 对象的 build(inputStream) 方法;
  public SqlSessionFactory build(InputStream inputStream) {
    return build(inputStream, null, null);
  }

  public SqlSessionFactory build(InputStream inputStream, String environment) {
    return build(inputStream, environment, null);
  }

  public SqlSessionFactory build(InputStream inputStream, Properties properties) {
    return build(inputStream, null, properties);
  }

  public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
     //2、 SqlSessionFactoryBuilder 会根据输入流 inputStream 等信息创建XMLConfigBuilder 对象 ;
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
    // 3、SqlSessionFactoryBuilder 调用 XMLConfigBuilder 对象的 parse() 方法;
    //  4、XMLConfigBuilder 对象返回 Configuration 对象;
      return build(parser.parse());
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error building SqlSession.", e);
    } finally {
      ErrorContext.instance().reset();
      try {
        inputStream.close();
      } catch (IOException e) {
        // Intentionally ignore. Prefer previous error.
      }
    }
  }

 // 5、SqlSessionFactoryBuilder 根据 Configuration 对象创建一个DefaultSessionFactory 对象;
  public SqlSessionFactory build(Configuration config) {
   //6、SqlSessionFactoryBuilder 返回 DefaultSessionFactory 对象给 Client ,供 Client使用。
    return new DefaultSqlSessionFactory(config);
  }

}

SqlSessionFactoryBuilder创建了DefaultSessionFactory对象,相当于SqlSessionFactory的构造器,采用了Builder设计模式([建造者设计模式].使用多个简单的对象一步一步构建一个复杂的对象.这种模式属于[创建型模式].目前它是创建对象的最佳模式.)

SqlSessionFactory基本介绍

SqlSessionFactory是每个MyBatis应用的核心,是MyBatis框架中的一个接口,主要负责MyBatis框架初始化操作以及为开发人员提供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();

}

SqlSessionFactory有两个实现类:

  • SqlSessionManager(废弃)
  • DefaultSqlSessionFactory
image

configuration

介绍

MyBatis框架支持开发人员通过配置文件与其进行交流.在配置文件所配置的信息,在框架运行时,会被XMLConfigBuilder解析并存储在一个Configuration对象中.Configuration对象会被作为参数传送给DeFaultSqlSessionFactory.而DeFaultSqlSessionFactory根据Configuration对象信息为Client创建对应特征的SqlSession对象

   private void parseConfiguration(XNode root) {
        try {
            this.propertiesElement(root.evalNode("properties"));
            Properties settings = this.settingsAsProperties(root.evalNode("settings"));
            this.loadCustomVfs(settings);
            this.typeAliasesElement(root.evalNode("typeAliases"));
            this.pluginElement(root.evalNode("plugins"));
            this.objectFactoryElement(root.evalNode("objectFactory"));
            this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
            this.reflectorFactoryElement(root.evalNode("reflectorFactory"));
            this.settingsElement(settings);
            this.environmentsElement(root.evalNode("environments"));
            this.databaseIdProviderElement(root.evalNode("databaseIdProvider"));
            this.typeHandlerElement(root.evalNode("typeHandlers"));
            this.mapperElement(root.evalNode("mappers"));
        } catch (Exception var3) {
            throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3);
        }
    }

原文地址

http://cbaj.gitee.io/blog/2020/07/22/mybatis%E6%BA%90%E7%A0%81%7CSessionFactory%E6%9C%BA%E5%88%B6%E5%8E%9F%E7%90%86/

相关文章

网友评论

      本文标题:mybatis源码|SessionFactory机制原理

      本文链接:https://www.haomeiwen.com/subject/fodxlktx.html