MyBatis源码解读(四)

292 阅读5分钟

3.3、MyBatis运行流程

3.3.1、配置解析

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

通过IO方式打开输入流,获取mybatis-config.xml以及xxxMapper.xml,因为在mybatis-config.xml中已经配置好了mapper文件的路径,所以在以一次的IO中把两者的信息都读到了。

那么在读取完配置文件以后,mybatis该如何处理所读取到的这些配置信息呢?在读取完mybatis-config.xml文件后,所有的配置都将会被封装成Configuration对象。那么此时问题又来了,如何将xml对象转为java对象呢?java提供了xml解析的一整套完整的结局方案。

xml解析在java中有3种方式,而mybatis采用的是第三种方式:

  1. dom
  2. sax
  3. xpath

在读取配置文件的这一行代码肯定会包含解析xml配置文件的步骤,所以我们跟进来看看这个build方法。

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

image-20230624122704490

接着点这个build方法。

image-20230624122727105

可以看到他是有解析这个xml的方法。我们点进去

image-20230628215228615

再点进去,我们会看到一个parseConfiguration方法,这个方法用于解析一个个的标签。

image-20230628215333713

随便抽出一行来解析可以看到,这个方法是用于解析mappers标签的方法。

 mapperElement(root.evalNode("mappers"));

image-20230628215534502

我们继续溯源,点mapperElement方法来剥丝抽茧。

image-20230628220148101

我们在写mappers标签的时候,如果写了packages属性(写dao接口所在的包的路径)的话,就不用再单独写一个个的dao类的路径,因为他会去扫描这个路径所在的所有的类。

 if ("package".equals(child.getName())) {
          String mapperPackage = child.getStringAttribute("name");
          configuration.addMappers(mapperPackage);
        }

如果我们不写packeags这个属性的话,就会走到else分支里面。

      String resource = child.getStringAttribute("resource");
          String url = child.getStringAttribute("url");
          String mapperClass = child.getStringAttribute("class");
          if (resource != null && url == null && mapperClass == null) {
            ErrorContext.instance().resource(resource);
            try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
              XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource,
                  configuration.getSqlFragments());
              mapperParser.parse();
            }
          } else if (resource == null && url != null && mapperClass == null) {
            ErrorContext.instance().resource(url);
            try (InputStream inputStream = Resources.getUrlAsStream(url)) {
              XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url,
                  configuration.getSqlFragments());
              mapperParser.parse();
            }
          } else if (resource == null && url == null && mapperClass != null) {
            Class<?> mapperInterface = Resources.classForName(mapperClass);
            configuration.addMapper(mapperInterface);
          } else {
            throw new BuilderException(
                "A mapper element may only specify a url, resource or class, but not more than one.");
          }

前三行其实是在获取我们写的属性。

     String resource = child.getStringAttribute("resource");
          String url = child.getStringAttribute("url");
          String mapperClass = child.getStringAttribute("class");

如果我们这样写标签的话

 <mapper resource="cn/linstudy/mapper/DeptMapper.xml"></mapper>

那么resource的值就是cn/linstudy/mapper/DeptMapper.xml,而url、mapperClass的值将会全部为空。然后就到了第一个if判断语句。

if (resource != null && url == null && mapperClass == null) {
     ErrorContext.instance().resource(resource);
            try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
              XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource,
                  configuration.getSqlFragments());
              mapperParser.parse();
            }
}

我们看到这个if条件语句可以得知,mapper标签里面的属性,只有resource是不可以为空,其他均可以为空。如果resource不为空的话就可以获取到这个属性对应的值。

然后我们就可以开始看parse这个方法,用于解析。

image-20230628224613423

然而负责解析maapper的是第三行。

 configurationElement(parser.evalNode("/mapper"));

所以我们接着顺藤摸瓜。

image-20230628224748278

我们可以看到这个方法是真正开始读取我们的xxxMapper.xml文件,也就是读取我们写的一些sql语句了。

其中这个方法最重要的是buildStatementFromContext(context.evalNodes("select|insert|update|delete"));这一句,我们点进去buildStatementFromContext方法。

image-20230628225332250

其中statementParser的parseStatementNode是将传过来的参数解析成mapperstatememt,我们来看看parseStatementNode方法。

image-20230628225736597

image-20230628225857751

我们看到这些红框标出来的有没有感到特别的熟悉呢?

image-20230628225952866

没错这些就是在mapper.xml文件中写的每一个标签里面的属性。

image-20230628230037064

我们再返回到parseStatementNode这个方法,我们可以发现前面的步骤都是在为了120行的addMappedStatement方法做数据准备。

image-20230628230235108

所以解析了那么多标签里面的数据,其实最终的·目的都是为了addMappedStatement来创建一个mapperstatement来准备数据的。我们进入这个方法。

image-20230628230438484

我们可以发现一个很经典的设计模式——构造者设计模式,构造者设计模式其实本质上是为了创建对象的,但凡使用了创建者模式创建对象的话,他创建对象的方法都是叫做build。

image-20230628230524893

这里就创建了一个MappedStatement对象,使用的也是创建者模式。

image-20230629222607896

在创建完mapperstatement对象后就存入到configuration对象里面去了。

    MappedStatement statement = statementBuilder.build();
    configuration.addMappedStatement(statement);

3.3.2、SqlSessionFactory

了解完配置的解析以后,我们来看看SqlSessionFactory的创建。我们找到我们写过的测试类,有一个build方法可以点进去。

image-20230629224217866

再点这个build方法。

image-20230629224242855

好家伙,还有一个build方法,我们可以看到build方法创建了一个默认的DefaultSqlSessionFactory对象。

image-20230629224319595

3.3.3、openSession

我们看完SqlSessionFactory创建以后,来看看sqlSessionFactory的openSession方法。

image-20230629224739526

然后我们发现这是一个接口方法。

image-20230629224836912

我们找第一个实现,因为SqlSessionFactory是由DefaultSqlSessionFactory创建的,所以我们找DefaultSqlSessionFactory实现的方法即可。

image-20230629224919310

点进去后是一个openSessionFromDataSource方法,我们可以发现一个特别有意思的现象,我们来看传入的参数是一个ExecutorType,也就是说虽然说是openSession,但是实际上真正干活的是Executor

image-20230629225052336

然后我们继续点进去这个方法,接着跟,这个就是创建sqlSession的核心。

image-20230702140622456

这里有一行很关键的代码。

image-20230707213959843

return new DefaultSqlSession(configuration, executor, autoCommit);

我们可以发现,创建一个DefaultSqlSession需要三个参数:

  1. configuration:提供各种配置信息
  2. executor:执行数据库的各种增删改查
  3. autoCommit:事务是自动提交