Configuration 、SqlSession、SqlSessionfactory源码分析

Configuration 、SqlSession、SqlSessionfactory源码分析

LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code,Keep progress,make a better result.
Survive during the day and develop at night。

目录

概 述

xml 配置

其实Configuration类是由XMLConfigBuilder(继承自BaseBuilder类)解析而来的,由如下方法(parseConfiguration)解析

 private void parseConfiguration(XNode root) {
    try {
      Properties settings = settingsAsPropertiess(root.evalNode("settings"));
      //issue #117 read properties first
      propertiesElement(root.evalNode("properties"));
      loadCustomVfs(settings);
      typeAliasesElement(root.evalNode("typeAliases"));
      pluginElement(root.evalNode("plugins"));
      objectFactoryElement(root.evalNode("objectFactory"));
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      reflectionFactoryElement(root.evalNode("reflectionFactory"));
      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);
    }
  }

回到parseConfiguration方法,该方法负责将XML文件中的信息解析到Configuration类的变量中,使其一一对应起来,下面是最后一个方法mapperElement(root.evalNode(“mappers”))的实现,负责读取节点,其他的方法也是同样的作用

Configuration类就像是MyBatis的总管,里面包含了所有的信息,有一些属性不设置也不会影响configuration的构建,因为MyBatis会给这些属性赋上默认值,以保证MyBatis能够正常运行。

二、各个配置解析

<properties resource="org/mybatis/example/config.properties">
  <property name="username" value="dev_user"/>
  <property name="password" value="F2Fa3!33TYyg"/>
</properties>

通过代码设置的configuration参数的优先级最高,因为他是在方法的最后面将那些值put进去的,然后就是properties子节点的优先级最低,最先被记载,也最容易被后面的参数覆盖,所以,如果采取读取外部文件的方式(resource或者url方式),子节点最好不要和其他参数重复。

映射器是MyBatis最复杂、最核心的组件。

 映射器提供DAO层接口到mapper.xml文件的映射,我们只需要调用DAO层的方法,就可以以执行对应的SQL语句,这里用到的是java的动态代理特性,我们先来看下config配置文件是中的mapper设置:


resource里面放的是mapper.xml文件,MyBatis会根据这个xml文件去解析里面的DAO层接口,以及里面的SQL语句和相应的设置,下面是parse()源码:

复制代码
public void parse() {
if (!configuration.isResourceLoaded(resource)) {
//这个解析mapper里面的各个元素,比如cache-ref、parameterMap、resultMap、sql、select|insert|update|delete等
configurationElement(parser.evalNode("/mapper"));
configuration.addLoadedResource(resource);
//这个方法是根据namespace将这个mapper和DAO层接口绑定起来
bindMapperForNamespace();
}
//这里面会分别解析…,见名知意了
parsePendingResultMaps();
parsePendingChacheRefs();
parsePendingStatements();
}
复制代码
这个parse()还是比较复杂的,特别是对resultMap标签的解析,以及对于 select|insert|update|delete 标签的解析,后续可能还会单独着重介绍下的。

6.其他的几个配置
当MyBatis在构建一个结果返回的时候,都会使用objectFactory去构建POJO,当然,我们也可以自己去定制自己的对象工厂,不过一般来说,使用默认的就够了。

sqlsession

SqlSession提供select/insert/update/delete方法,在旧版本中使用使用SqlSession接口的这些方法,但是新版的Mybatis中就会建议使用Mapper接口的方法。

映射器其实就是一个动态代理对象,进入到MapperMethod的execute方法就能简单找到SqlSession的删除、更新、查询、选择方法,从底层实现来说:通过动态代理技术,让接口跑起来,之后采用命令模式,最后还是采用了SqlSession的接口方法(getMapper()方法等到Mapper)执行SQL查询(也就是说Mapper接口方法的实现底层还是采用SqlSession接口方法实现的)。

(2)SqlSession重要的四个对象

1Execute:调度执行StatementHandlerParmmeterHandlerResultHandler执行相应的SQL语句;

    2StatementHandler:使用数据库中StatementPrepareStatement)执行操作,即底层是封装好了的prepareStatement;

    3ParammeterHandler:处理SQL参数;

    4ResultHandler:结果集ResultSet封装处理返回。

执行器起到至关重要的作用,它是真正执行Java与数据库交互的东西,参与了整个SQL查询执行过程中。

1)主要有三种执行器:简易执行器SIMPLE(不配置就是默认执行器)、REUSE是一种重用预处理语句、BATCH批量更新、批量专用处理器

2)执行器作用:Executor会先调用StatementHandlerprepare()方法预编译SQL语句,同时设置一些基本的运行参数,然后调用StatementHandlerparameterize()方法(实际上是启用了ParameterHandler设置参数)设置参数,resultHandler再组装查询结果返回调用者完成一次查询完成预编译,简单总结起来就是即先预编译SQL语句,之后设置参数(跟JDBC的prepareStatement过程类似)最后如果有查询结果就会组装返回。

第一:Executor通过Configuration对象中newExecutor()方法中选择相应的执行器生成

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    if (ExecutorType.BATCH == executorType) {
      executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
      executor = new ReuseExecutor(this, transaction);
    } else {
      executor = new SimpleExecutor(this, transaction);
    }
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  } 

第二:在执行器中StatementHandler是根据Configuration构建的

public SimpleExecutor(Configuration configuration, Transaction transaction) {
    super(configuration, transaction);
  }

  @Override
  public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
    Statement stmt = null;
    try {
      Configuration configuration = ms.getConfiguration();
      StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
      stmt = prepareStatement(handler, ms.getStatementLog());
      return handler.update(stmt);
    } finally {
      closeStatement(stmt);
    }
  }

第三:Executor会执行StatementHandler的prepare()方法进行预编译---->填入connection对象等参数---->再调用parameterize()方法设置参数---->完成预编译

private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
    Statement stmt;
    Connection connection = getConnection(statementLog);
    stmt = handler.prepare(connection, transaction.getTimeout());
    handler.parameterize(stmt);
    return stmt;
  } 

(2)StatementHanlder数据库会话器

1)作用:简单来说就是专门处理数据库会话。详细来说就是进行预编译并且调用ParameterHandler的setParameters()方法设置参数。

2)数据库会话器主要有三种:SimpleStatementHandler、PrepareStatementHandler、CallableStatementHandler,分别对应Executor的三种执行器(SIMPLE、REUSE、BATCH)

第一:StatementHandler的生成是由Configuration方法中newStatementHandler()方法生成的,但是正在创建的是实现了StatementHandler接口的RoutingStatementHandler对象

第二:RoutingStatementHandler的通过适配器模式找到对应(根据上下文)的StatementHandler执行的,并且有SimpleStatementHandler、PrepareStatementHandler、CallableStatementHandler,分别对应Executor的三种执行器(SIMPLE、REUSE、BATCH)

public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {

switch (ms.getStatementType()) {
  case STATEMENT:
    delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
    break;
  case PREPARED:
    delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
    break;
  case CALLABLE:
    delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
    break;
  default:
    throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
}

SqlSessionfactory:
1.SqlSessionFactory简介
SqlSessionFactory是MyBatis中的一个重要的对象,它是用来创建SqlSession对象的,而SqlSession用来操作数据库的。

2.SqlSessionFactory生成
SqlSessionFactory对象可以通过SqlSessionFactoryBuilder来获得,而SqlSessionFactoryBuildr则可以从XML配置文件或预先定制的Configuration实例构建出SqlSessionFactory的实例。

SqlSessionFactory是线程安全的,它一旦被创建,应该在应用执行期间都存在。在应用运行期间不要重复创建多次,建议使用单例模式。

3.SqlSessionFactory的常用方法
SqlSessionFactory主要用于创建SqlSession,一般是从Connection或者DataSource中创建。

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();

}

总结:

相关工具如下:

分析:

小结:

主要讲述了Configuration 、SqlSession、SqlSessionfactory源码分析,请大家指正~

参考资料和推荐阅读

1.链接: 参考资料.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值