Mybatis中创建SqlSession的流程

我们在使用Mybatis进行CRUD操作之前都会去获取SqlSession对象,没有SqlSession对象就无法进行接下来的操作

public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory;
    static{
        try {
            String resource = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }

}

可以看到,我们是很简单的就获取了Sqlsession对象,好像很轻易就得到它一样,看似没几步,但是实际步骤要比这稍微复杂些,那就探究分析下获取sqlSession的完整步骤吧

1.通过Resources加载全局配置文件

Resource类apache.ibatis.io包下面的一个类,通过io流读取配置文件,把配置文件解析成一个InputSream输入流对象

2.实例化sqlSessionFactoryBulider

通过build方法去读取Mybatis的配置文件,
由XMLConfigBuilder解析配置文件流,通过parser.parse(),将配置信息放置在configuration中,创建DefaultSessionFactory对象,DefaultSessionFactory是SqlSessionFactory的实现类


  public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
    //由xml解析配置文件流,通过parser.parse(),将解析的配置信息放置在configuration中
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
      //在这里通过调用buid方法获取DefalutSessionFactory对象
      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.
      }
    }
  }

  public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }
}

看看都解析了configuration中的哪些节点

 private void parseConfiguration(XNode root) {
    try {
      //issue #117 read properties first
      propertiesElement(root.evalNode("properties"));
      Properties settings = settingsAsProperties(root.evalNode("settings"));
      loadCustomVfs(settings);
      loadCustomLogImpl(settings);
      typeAliasesElement(root.evalNode("typeAliases"));
      pluginElement(root.evalNode("plugins"));
      objectFactoryElement(root.evalNode("objectFactory"));
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      reflectorFactoryElement(root.evalNode("reflectorFactory"));
      settingsElement(settings);
      // read it after objectFactory and objectWrapperFactory issue #631
      environmentsElement(root.evalNode("environments"));
      databaseIdProviderElement(root.evalNode("databaseIdProvider"));
      typeHandlerElement(root.evalNode("typeHandlers"));
      mapperElement(root.evalNode("mappers"));
    } catch (Exception e) {
      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);
    }
  }

3.通过openSession()获取SqlSession对象

进入DefaultSessionFactory中发现openSession()里面调用了openSessionFromDataSource方法
在这里插入图片描述
看看openSessionFromDataSource()

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
     //通过Confuguration对象去获取Mybatis相关配置的信息
      final Environment environment = configuration.getEnvironment();
      //由TransactionFactory创建一个Transaction事务对象
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      //创建执行器executor
      final Executor executor = configuration.newExecutor(tx, execType);
      //关键点!创建DefaultSqlSession对象
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } 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();
    }
  }

表面上,我们是通过sqlSession来执行SQL语句,实际上SQL是通过Excutor执行器执行,因为Excutor含有大量的提交,回滚,关闭资源的方法,如图:通过上面源码发现得到了DefaultSqlSession对象, 而DefaultSqlSession是SqlSession接口的实现类,也就说明得到了sqlSession
SqlSession里面存在Transation事务,执行sql语句是要涉及到事务的,如果执行成功就提交,如果执行失败就回滚。
在这里插入图片描述
得到了sqlSession,接下来我们就可以该干嘛干嘛了(CRUD)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值