解释MyBatis主要语句源码流程图

对于mybatis 来讲 整体的流程大体为下图

在这里插入图片描述
本文主要解析从获取配置文件 到获取mapper对象


InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = factory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);

一、获取配置文件的文件输入流

InputStream is = Resources.getResourceAsStream("mybatis-config.xml");

该部分流程图
在这里插入图片描述

Resource 资源工具类
作用 : 将路径下的资源文件读取到流中

首先通过getResourceAsStream 读取资源文件

class:Resources
public static InputStream getResourceAsStream(String resource) throws IOException {
    return getResourceAsStream(null, resource);
  }

  /*
   * Returns a resource on the classpath as a Stream object
   *
   * @param loader   The classloader used to fetch the resource
   * @param resource The resource to find
   * @return The resource
   * @throws java.io.IOException If the resource cannot be found or read
   */
  public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
    InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
    if (in == null) {
      throw new IOException("Could not find resource " + resource);
    }
    return in;
  }

在读取的过程中第哦啊用classLoaderWapper中的方法

class : ClassLoaderWrapper



  /*
   * Returns a resource on the classpath as a Stream object
   *
   * @param resource The resource to find
   * @return The resource
   * @throws java.io.IOException If the resource cannot be found or read
   */
  public static InputStream getResourceAsStream(String resource) throws IOException {
    return getResourceAsStream(null, resource);
  }


 /*
   * Returns a resource on the classpath as a Stream object
   *
   * @param loader   The classloader used to fetch the resource
   * @param resource The resource to find
   * @return The resource
   * @throws java.io.IOException If the resource cannot be found or read
   */
  public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
    InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
    if (in == null) {
      throw new IOException("Could not find resource " + resource);
    }
    return in;
  }


/*
   * Get a resource from the classpath, starting with a specific class loader
   *
   * @param resource    - the resource to find
   * @param classLoader - the first class loader to try
   * @return the stream or null
   */
  public InputStream getResourceAsStream(String resource, ClassLoader classLoader) {
    return getResourceAsStream(resource, getClassLoaders(classLoader));
  }

对传进的classLoder[ ] 这个类加载器的数组进行遍历 类判断类加载其中的流是否为null
如果不为空 则返回一个InputStream 输入流对象

class: ClassLoaderWrapper
 /*
   * Try to get a resource from a group of classloaders
   *
   * @param resource    - the resource to get
   * @param classLoader - the classloaders to examine
   * @return the resource or null
   */
  InputStream getResourceAsStream(String resource, ClassLoader[] classLoader) {
    for (ClassLoader cl : classLoader) {
      if (null != cl) {

        // try to find the resource as passed
        InputStream returnValue = cl.getResourceAsStream(resource);

        // now, some class loaders want this leading "/", so we'll add it and try again if we didn't find the resource
        if (null == returnValue) {
          returnValue = cl.getResourceAsStream("/" + resource);
        }

        if (null != returnValue) {
          return returnValue;
        }
      }
    }
    return null;
  }

获取SqlsessionFactory

SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

**SqlsessionFactory是mybatis的 核心对象,获取核心对象的方式
SqlsessionFactoryBuilder构建 SqlsessionFactory 实例
通过Xpath解析的方式去解析mybatis-config.xml 文件 解析的文件内容套接到configuration中 而这个configuration 相当于 mybatis-config.xml 中的配置文件所对应的类。返回DefaultSqlSessionFactory对象。
**

流程在这里插入图片描述

构建SqlSessionFactory示例

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




通过Xpath解析的方式去解析mybatis-config.xml 文件 解析的文件内容套接到configuration中 而这个configuration 相当于 mybatis-config.xml 中的配置文件所对应的类。返回DefaultSqlSessionFactory对象

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
      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);
  }


三、获取SqlSession对象

**获取SQLsession对象 通过DefaultSqlSessionFactory对象调用它里面opensession方法返回DefaultSQLSession对象
调用的是DefaultSqlSessionFactory类中opensessionFromDataSource
该方法下有三个参数 第一个执行器的类型 第二个 事务的隔离级别 第三个 是否自动提交
**
*
流程
在这里插入图片描述

获取SQLsession对象 通过DefaultSqlSessionFactory对象调用它里面opensession方法返回DefaultSQLSession对象

 @Override
  public SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level) {
    return openSessionFromDataSource(execType, level, false);
  }

 private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
 //*声明一个事务的对象
    Transaction tx = null;
    try {
   
    //通过配置文件去读取环境标签的信息
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      //通过dataSource的配置获取事务的对象
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      //,再根据事务执行器的类型 去创建事务执行器 Executor(相当于Statement)通过执行器
      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获取相应Mapper

**第一层通过调用sqlsession中getMapper方法
第二层通过调用配置中的getMapper方法
第三层通过映射的注册器中的getMapper方法来进行获取相应的Mapper对象
其中使用了MapperProxyFactory 工厂对象来获取 MapperProxy 在里面使用反射和动态代理的方式来获取最终的mapper对象。

**

流程
在这里插入图片描述
通过调用sqlsession中getMapper方法

 @Override
  public <T> T getMapper(Class<T> type) {
    return getConfiguration().getMapper(type, this);
  }

通过调用配置中的getMapper方法

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    return mapperRegistry.getMapper(type, sqlSession);
  }

通过映射的注册器中的getMapper方法来进行获取相应的Mapper对象

 public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
    final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
    if (mapperProxyFactory == null) {
      throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
    }
    try {
      return mapperProxyFactory.newInstance(sqlSession);
    } catch (Exception e) {
      throw new BindingException("Error getting mapper instance. Cause: " + e, e);
    }
  }

MapperProxyFactory 工厂对象来获取 MapperProxy 在里面使用反射和动态代理的方式来获取最终的mapper对象。

 public T newInstance(SqlSession sqlSession) {
    final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache);
    return newInstance(mapperProxy);
  }

总结


一、通过getResourceAsStream  读取资源文件 
在读取的过程中 调用 classLoaderWrapper中的方法
然后通过对classLoader[]数组的遍历 然后进行判断 类加载器中所读的流是否为null
如果不为null 则返回InputStream对象

二、
SqlsessionFactory是mybatis的 核心对象,获取核心对象的方式
SqlsessionFactoryBuilder构建  SqlsessionFactory 实例
通过Xpath解析的方式去解析mybatis-config.xml 文件 解析的文件内容套接到configuration中 而这个configuration 相当于 mybatis-config.xml 中的配置文件所对应的类。返回DefaultSqlSessionFactory对象。


三、
获取SQLsession对象 通过DefaultSqlSessionFactory对象调用它里面opensession方法返回DefaultSQLSession对象
调用的是DefaultSqlSessionFactory类中opensessionFromDataSource



四、通过sqlsession获取相应Mapper
   第一层通过调用sqlsession中getMapper方法
   第二层通过调用配置中的getMapper方法
   第三层通过映射的注册器中的getMapper方法来进行获取相应的Mapper对象
其中使用了MapperProxyFactory 工厂对象来获取 MapperProxy 在里面使用反射和动态代理的方式来获取最终的mapper对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值