mybatis source series Configuration class Configuration

Posted by bentobenji on Sun, 10 Nov 2019 15:59:52 +0100

In Chapter 1, we learned that the core configuration class of mybatis is org.apache.ibatis.session.Configuration.java

Let's first look at the class diagram of Configuration.java

Seeing the class diagram, I was stunned. There are too many properties

However, considering that mybatis has such powerful functions and so many attributes are understandable, I will explore them one by one

Constructor

Through the class diagram, we find that Configuration mainly provides two constructors:

  • Empty construction, no parameters passed
  • Construct according to Environment environment variable

Empty structure

First, look at the code of the construction

/***
   * Empty construction, initializing related processing class methods of mybatis
   */
  public Configuration() {
    typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
    typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);

    typeAliasRegistry.registerAlias("JNDI", JndiDataSourceFactory.class);
    typeAliasRegistry.registerAlias("POOLED", PooledDataSourceFactory.class);
    typeAliasRegistry.registerAlias("UNPOOLED", UnpooledDataSourceFactory.class);

    typeAliasRegistry.registerAlias("PERPETUAL", PerpetualCache.class);
    typeAliasRegistry.registerAlias("FIFO", FifoCache.class);
    typeAliasRegistry.registerAlias("LRU", LruCache.class);
    typeAliasRegistry.registerAlias("SOFT", SoftCache.class);
    typeAliasRegistry.registerAlias("WEAK", WeakCache.class);

    typeAliasRegistry.registerAlias("DB_VENDOR", VendorDatabaseIdProvider.class);

    typeAliasRegistry.registerAlias("XML", XMLLanguageDriver.class);
    typeAliasRegistry.registerAlias("RAW", RawLanguageDriver.class);

    typeAliasRegistry.registerAlias("SLF4J", Slf4jImpl.class);
    typeAliasRegistry.registerAlias("COMMONS_LOGGING", JakartaCommonsLoggingImpl.class);
    typeAliasRegistry.registerAlias("LOG4J", Log4jImpl.class);
    typeAliasRegistry.registerAlias("LOG4J2", Log4j2Impl.class);
    typeAliasRegistry.registerAlias("JDK_LOGGING", Jdk14LoggingImpl.class);
    typeAliasRegistry.registerAlias("STDOUT_LOGGING", StdOutImpl.class);
    typeAliasRegistry.registerAlias("NO_LOGGING", NoLoggingImpl.class);

    typeAliasRegistry.registerAlias("CGLIB", CglibProxyFactory.class);
    typeAliasRegistry.registerAlias("JAVASSIST", JavassistProxyFactory.class);

    languageRegistry.setDefaultDriverClass(XMLLanguageDriver.class);
    languageRegistry.register(RawLanguageDriver.class);
  }

A large number of built-in mybatis core logic processing classes are registered in the empty structure. The functions of these classes will be studied one by one in the following chapters, which will not be explained here

According to Environment

Look at constructors

/***
   * Construction according to environmental parameters
   * @param environment
   */
  public Configuration(Environment environment) {
    this();
    this.environment = environment;
  }

Call the empty construct first, and then assign the Environment property

If you don't look at the specific properties, the Configuration configuration is similar to the target two constructors

We will study the properties of Configuration one by one later

Topics: Mybatis Java Apache Session