Talk about the initial builder mode of Mybatis

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

Talk about the initial builder mode of Mybatis

Mybatis will first load and parse the global configuration file and call the SqlSessionFactoryBuilder.build() method:

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.
      }
    }
  }
  public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config);
  }
复制代码
  1. Constructs an XMLConfigBuilder object,
  2. Call the parser.parse() method to parse the xml file and return the Configuration object. All configuration information of the configuration file is stored in the Configuration object.
  3. Call the build() method to generate the DefaultSqlSessionFactory object and return

Let's take a look at the parse() method of XMLConfigBuilder:

public Configuration parse() {
    if (parsed) {
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    parseConfiguration(parser.evalNode("/configuration"));
    return 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);
    }
  }
复制代码

Parse the properties tag in the xml, this tag generally defines the url to connect to the database, drives the username and password, etc., in the form of KV, and is stored in the object of the Properties class

Parse the settings tag in xml, this tag is the global configuration of mybatis, such as enabling secondary cache, lazy loading, etc., and save it in the Properties class

Parse the typeAliases tag in xml, this tag is used to set the alias of the class and record it to the TypeAliasRegistry object

Parse the typeHandler tag in xml, this tag is used to set the type converter, the type converter is to realize the type conversion between java and jdbc, which is recorded in the TypeHandlerRegistry

Parse the plugins tag in xml. This tag can be intercepted and called at a certain point in the execution process. The custom class can implement the Interceptor interface and save it in the Configuration object.

Parse the object factory objectFactory tag in xml. This tag can be used to configure a custom object factory. When used, it inherits the DefaultObjectFactory class of Mybatis. It will be called when the result object instance is created. This parsing is also saved in the Configuration object.

Parse the object wrapper factory objectWrapperFactory tag in xml

Parse the reflectorFactory tag that creates and caches Reflector objects in xml, you can customize the factory class to inherit the DefaultReflectorFactory class

Parse the environments tag in xml, this tag is used to configure different environments, including transactionManager transaction manager tag and dataSource data source tag

Parse the databaseIdProvider tag in xml, this tag is the identifier of the database vendor, the parsed tag is saved in the DatabaseIdProvider object, call getDatabaseId() to get the databaseId to determine the database vendor

Parse the mappers tag, the mappers tag must be familiar to everyone, it is the corresponding xml file

Summarize

This article mainly introduces the content of Mybatis parsing the tags of the global configuration file, and uses the construction mode to build the Configuration object

Guess you like

Origin juejin.im/post/7079401491903021093