mybatis source learning

Keywords: JDBC Mybatis xml MySQL

mybatis global configuration file loading

In the previous article, SqlSession FactoryBuilder was mentioned to load various resources in the configuration file by calling the parse method in the XML ConfigBuilder. In the parse method, the parseConfiguration method is mainly called to load the configuration information. The parseConfiguration method is as follows:

	private void parseConfiguration(XNode root) {
	  try {
	    //issue #117 read properties first
	    propertiesElement(root.evalNode("properties"));
	    Properties settings = settingsAsProperties(root.evalNode("settings"));
	    loadCustomVfs(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);
	  }
	}
  1. ProperrtiesElement, this method is used to load property variable information, which includes url, driver, username,password and other information. It is loaded by reading the information of the < properties node in the configuration file.
  private void propertiesElement(XNode context) throws Exception {
    if (context != null) {
      //
      Properties defaults = context.getChildrenAsProperties();
      String resource = context.getStringAttribute("resource");
      String url = context.getStringAttribute("url");
      if (resource != null && url != null) {
        throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference.  Please specify one or the other.");
      }
      if (resource != null) {
        defaults.putAll(Resources.getResourceAsProperties(resource));
      } else if (url != null) {
        defaults.putAll(Resources.getUrlAsProperties(url));
      }
      Properties vars = configuration.getVariables();
      if (vars != null) {
        defaults.putAll(vars);
      }
      parser.setVariables(defaults);
      configuration.setVariables(defaults);
    }
  }

As you can see from the above code, it reads the value of resource or url, and then loads the configuration information based on this value, because some configurations are like this.

<configuration>
    <properties resource="jdbc.properties"  />
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/LeaveMapper.xml"></mapper>
    </mappers>
</configuration>

The jdbc. properties file is as follows

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.197.205:3306/system_data_permission?characterEncoding=utf8
username=root
password=toor@gzstrong

In this way, the database-related configuration file information is placed in a separate property. When the system records, it reads the configuration properties file information and then fills in the following variables. Look again at the properties Element method, which uses Properties defaults = context.getChildrenAsProperties(); the method obtains all the sub-node information under this node, because some mybatis global configuration files do not contain configuration information in properties, but write it directly in < properties In this node, for example:

    <properties>
        <property name="driver" value="driver"/>
        <property name="url" value="url"/>
        <property name="username" value="username"/>
        <property name="password" value="password"/>
    </properties>

Then read the resource's file name to get the property information according to the property node's attribute, or get the information in the url. In this case, <property node, the resource and URL attributes can not coexist, and then read the relevant configuration information according to the configuration information is URL or resource. After reading, it determines whether the variable property exists in the original configuration object, which is set according to actual needs at the beginning of initialization.
From the point of view of code, properties have loading order, which is
1. Properties set at initialization
2. Order of configuration information on resource or url
3. The order of the subnodes of the properties node itself.

Posted by leenoble_uk on Sat, 10 Aug 2019 00:55:31 -0700