Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Mybatis source code analysis [02.XMLConfigBuilder]

Shulou Source: shulou.com Published: 2022-06-03 06:27:26 11月23日 Update

/ / BaseBuilder attribute public abstract class BaseBuilder {/ / requires configuration, type alias registration, type handler registration 3 things protected final Configuration configuration; protected final TypeAliasRegistry typeAliasRegistry; protected final TypeHandlerRegistry typeHandlerRegistry;}

Public class XMLConfigBuilder extends BaseBuilder {}, it can be seen that XMLConfigBuilder inherits from BaseBuilder

/ / XMLConfigBuilder attribute public class XMLConfigBuilder extends BaseBuilder {/ / whether the private boolean parsed; / / XPath parser private XPathParser parser; / / current environment private String environment;} has been parsed

SqlSessionFactoryBuilder calls the parse () function of XMLConfigBuilder

/ parsing configuration public Configuration parse () {/ / if you have already parsed, error if (parsed) {throw new BuilderException ("Each XMLConfigBuilder can only be used once.") } parsed = true; / / Root node is configuration parseConfiguration (parser.evalNode ("/ configuration")); return configuration;}

You can see from the source code that the parse () function is actually a called parseConfiguration () function, and the internal implementation is as follows:

Private void parseConfiguration (XNode root) {try {/ / step by step parsing / / issue # 117 read properties first / / 1.properties propertiesElement (root.evalNode ("properties")); / / 2. Type alias typeAliasesElement (root.evalNode ("typeAliases")); / / 3. Plug-in pluginElement (root.evalNode ("plugins")); / / 4. Object factory objectFactoryElement (root.evalNode ("objectFactory")); / / 5. Object packaging factory objectWrapperFactoryElement (root.evalNode ("objectWrapperFactory")); / / 6. Set settingsElement (root.evalNode ("settings")); / / read it after objectFactory and objectWrapperFactory issue # 631 / / 7. Environment environmentsElement (root.evalNode ("environments")); / / 8.databaseIdProvider databaseIdProviderElement (root.evalNode ("databaseIdProvider")); / / 9. Type processor typeHandlerElement (root.evalNode ("typeHandlers")); / / 10. Mapper mapperElement (root.evalNode ("mappers"));} catch (Exception e) {throw new BuilderException ("Error parsing SQL Mapper Configuration. Cause:" + e, e);}}

The above function is for mybatis to parse the key nodes of the configuration file.

PropertiesElement (XNode context) / typeAliasesElement (XNode context) / pluginElement (XNode context) / / MyBatis allows you to intercept calls executed by mapped statements at some point. By default MyBatis allows the use of plug-ins to intercept method calls / objectFactoryElement (XNode context) / objectWrapperFactoryElement (XNode context) settingsElement (XNode context) / environmentsElement (XNode context) / / databaseIdProviderElement (XNode context) / typeHandlerElement (XNode context) / mapperElement (XNode context) / / 1. Use the classpath / 2. Use absolute url path / 3. Use java class name / 4. Automatically scan all mappers under the package / /

Tags: Functions types processing configuration aliases processors objects emitters properties factories plug-ins environments nodes paths source code things key actual actually situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information Apple OPPO Reno MySQL