[Mad God Mybatis Notes] Configuration Analysis

Core configuration file: mybatis-config.xml

Mybatis configuration files contain settings and property information that affect the behavior of Mybatis

Environment configuration (environments)

Requirements: Learn to use and configure multiple operating environments

MyBatis can adapt to a variety of environments. Although multiple environments can be configured, only one environment can be selected per SqlSessionFactory.

 If you want to switch the environment, you can modify the default value.

 transactionManager : transaction manager

There are two types of transaction managers in MyBatis (understand)

JDBC: Direct use of JDBC's commit and rollback settings, relying on connections from the data source to manage transaction scope.

 If you use Spring+Mybatis, there is no need to configure the transaction manager, because the Spring module will overwrite the previous configuration with its own module.

Data source: datesource

The dataSource element uses the standard JDBC data source interface to configure the resources of the JDBC connection object

There are three built-in data source types

UNPOOLED: (no connection pool)

The connection is opened and closed each time it is requested. Although a bit slow, it's a good choice for simple applications that don't have high requirements in terms of database connection availability.

Different databases have different performance in terms of performance, and for some databases, it is not important to use connection pooling

, this configuration is very suitable for this situation.

UNPOOLED properties:

 POOLED: (default)

This data source implementation utilizes the concept of pools to organize JDBC connection objects, avoiding the need to create new connection instances

Initialization and authentication time, which is a popular way of making concurrent web applications respond quickly to requests.

In addition to the properties mentioned above under UNPOOLED, there are more properties to configure the data source of POOLED

properties attribute:

The configuration file can be referenced through the properties attribute

These properties are externally configurable and dynamically replaceable , either configured in a typical java properties file or passed through a child element of the properties element

Write a configuration file:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?userSSL=true&userUnicode=true&characterEncoding=UTF-8
username=root
password=

Introduced in the core configuration file:

 In case of conflict, the external configuration file will be used first

TypeAliases:

Type alias is to set a short name for java type, it is only related to xml configuration, the meaning of existence is only to reduce the redundancy of fully qualified name.

<typeAliases>
   <typeAlias type="包名.类名" alias="类名">
</typeAliases>
<typeAliases>
   <package="包名"/>
</typeAliases>

 

 

 

 

 When there are few entity classes, the first one is used; the second one is used more frequently; the first one can customize aliases, and the second one cannot customize aliases.

But in the case of scanning the package, you can write an annotation on the class to create an alias to implement a custom alias:

 

 

set settings

cacheEnabled(true|false) Globally enables or disables any cache that has been configured by all mappers in the configuration file.

lazyLoadingEnabled(true|false) Global switch for lazy loading. When on, all associated objects are lazy loaded. The switch state of this item can be overridden by setting the fetchType property in a specific relationship.

logImpl: Specify the specific implementation of the log used by MyBatis, it will be automatically searched if not specified

SLF4J|LOG4J|LOG4J2|JDK_LOGGING|
COMMONS_LOGGING|
STDOUT_LOGGING|
NO_LOGGING

mapper mappers:

MapperRegistry: Register binding Mapper file

Use classpath-relative resource references

 Use class file binding registration:

Note: When using class registration, the interface and the Mapper configuration file must have the same name; the interface and the Mapper configuration file must be under the same package

Register with package binding:

Note: Use package to register bindings, the same as class registration.

Resource binding is recommended 

Scope and declaration cycle:

Lifetime and scope are critical because incorrect usage can lead to serious concurrency problems.

● Once the SqlSessionFactory is created, it is no longer needed. SqlSessionFactory is a local variable

●SqlSessionFactory:

It can be understood as a database connection pool.

Once created, the SqlSessionFactory should exist for the duration of the application's runtime, there is no reason to drop or recreate another instance. The best scope for SqlSessionFactory is application scope.

There are many ways to do it, the easiest is to use singleton pattern or static pattern.

●SqlSession:

It can be understood as a request to connect to the connection pool, which needs to be opened and closed, otherwise resources will be occupied. Each thread should have its own instance of SqlSession. SqlSession instances are not thread-safe and therefore cannot be shared, so their optimal scope is request or method scope.

 Each mapper represents a specific business

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124351043