SoFunction
Updated on 2025-04-12

MyBatis configuration object configuration function and description

MyBatis configuration object Configuration function

It is the core configuration object of MyBatis, which contains almost all the configuration information required for the MyBatis runtime. It can be understood as the "brain" or "configuration center" of MyBatis.

During the initialization of MyBatis, the XML configuration file is parsed or a Java code is builtConfigurationObject, other components of MyBatis (e.g.SqlSessionFactoryExecutorMappedStatementWill rely on thisConfigurationObject to obtain configuration information and perform operations.

The main role of Configuration object

Save global configuration information: ConfigurationThe object saves the global configuration information of MyBatis, which is usually inConfigure in the file, or set through Java code. These information include:

  • Properties:External attribute configuration (can be passed<properties>Tag orPropertiesObject loading).
  • Settings:Global settings control the runtime behavior of MyBatis (for example, whether to enable cache, whether to use lazy loading, whether to automatically map, etc.).
  • Type Aliases:Type alias, used to simplify type references in XML configuration files.
  • Type Handlers:Type processor for handling conversion between Java types and JDBC types.
  • Object Factory:Object factory, used to create result objects.
  • Plugins:Plugin, used to intercept the core execution process of MyBatis (for example, intercept SQL execution, parameter settings, result mapping, etc.).
  • Environments:Environment configuration, you can configure multiple data source environments (for example, development environment, test environment, production environment).
  • DatabaseId Provider:Database vendor identifier, used to execute different SQL statements according to different database vendors.
  • Mappers:Mapper configuration to load SQL mapped files or Mapper interfaces.

Manage MappedStatement: ConfigurationThe object maintains aMappedStatementRegistration form (Map<String, MappedStatement>)。MappedStatementThe object represents a SQL statement (selectinsertupdatedelete), which contains the ID, parameter type, result type, SQL statement content and other information of the SQL statement.ConfigurationObject passedMappedStatementID to find and manageMappedStatement

Create core components: ConfigurationObjects provide some methods to create core components of MyBatis, such as:

  • newExecutor(Transaction tx, ExecutorType executorType): CreateExecutorObject (executor, responsible for executing SQL statements).
  • newStatementHandler(...): CreateStatementHandlerObject (statement processor, responsible for handling JDBCStatement)。
  • newParameterHandler(...): CreateParameterHandlerObject (parameter processor, responsible for setting parameters of SQL statements).
  • newResultSetHandler(...): CreateResultSetHandlerObject (result set processor, responsible forResultSetMap to Java objects).

Access interface that provides configuration information: ConfigurationThe object provides a series ofgetXXX()Method, used to obtain various configuration information. For example:

  • getVariables(): Get the attribute configuration
  • isCacheEnabled(): Get whether to enable cache.
  • isLazyLoadingEnabled(): Get whether to enable delayed loading.
  • getTypeAliasRegistry(): Get the type alias registry.
  • getTypeHandlerRegistry(): Get the type processor registry.
  • getMappedStatement(String id): Obtained by IDMappedStatementObject.
  • getEnvironment(): Obtain the environment configuration

The entrance to the plug-in mechanism: ConfigurationProvides methods to add and get plugins (addInterceptor(), getInterceptors()), allowing the plug-in to intercept the core execution process of MyBatis.

Configuration object creation

  • ConfigurationObjects are usually made fromSqlSessionFactoryBuildercreate.
  • SqlSessionFactoryBuilderCan be built with XML configuration files or Java codeConfigurationObject.
// Create through XML configuration filesString resource = "";
InputStream inputStream = (resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
Configuration configuration = ();


// Create through Java codeConfiguration configuration = new Configuration();
(true); // Set cache(false); // Set delay loading(); // Add Mapper interface// ... Other configurations ...SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

Use of Configuration objects (usually no direct operation is required)

  • In most cases, we do not need to operate directlyConfigurationObject.
  • MyBatis will be used internallyConfigurationObject to perform various operations.

Summarize

ConfigurationThe object is the core configuration object of MyBatis, which contains almost all the configuration information required by the MyBatis runtime and is responsible for managing it.MappedStatement, create core components, provide access interfaces to configuration information, and serve as an entrance to the plug-in mechanism.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.