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 builtConfiguration
Object, other components of MyBatis (e.g.SqlSessionFactory
、Executor
、MappedStatement
Will rely on thisConfiguration
Object to obtain configuration information and perform operations.
The main role of Configuration object
Save global configuration information: Configuration
The 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 orProperties
Object 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: Configuration
The object maintains aMappedStatement
Registration form (Map<String, MappedStatement>
)。MappedStatement
The object represents a SQL statement (select
、insert
、update
、delete
), which contains the ID, parameter type, result type, SQL statement content and other information of the SQL statement.Configuration
Object passedMappedStatement
ID to find and manageMappedStatement
。
Create core components: Configuration
Objects provide some methods to create core components of MyBatis, such as:
-
newExecutor(Transaction tx, ExecutorType executorType)
: CreateExecutor
Object (executor, responsible for executing SQL statements). -
newStatementHandler(...)
: CreateStatementHandler
Object (statement processor, responsible for handling JDBCStatement
)。 -
newParameterHandler(...)
: CreateParameterHandler
Object (parameter processor, responsible for setting parameters of SQL statements). -
newResultSetHandler(...)
: CreateResultSetHandler
Object (result set processor, responsible forResultSet
Map to Java objects).
Access interface that provides configuration information: Configuration
The 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 IDMappedStatement
Object. -
getEnvironment()
: Obtain the environment configuration
The entrance to the plug-in mechanism: Configuration
Provides methods to add and get plugins (addInterceptor()
, getInterceptors()
), allowing the plug-in to intercept the core execution process of MyBatis.
Configuration object creation
-
Configuration
Objects are usually made fromSqlSessionFactoryBuilder
create. -
SqlSessionFactoryBuilder
Can be built with XML configuration files or Java codeConfiguration
Object.
// 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 directly
Configuration
Object. - MyBatis will be used internally
Configuration
Object to perform various operations.
Summarize
Configuration
The 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.