Mybatis [configuration analysis]

4. Configuration analysis

1. Core configuration file

mybatis-config.xml

MyBatis configuration files contain information about settings and properties that deeply affect MyBatis behavior.

    configuration (configuration)
    properties (attribute)
    settings (setting)
    typeAliases (type alias)
    typeHandlers (type processor)
    objectFactory (object factory)
    plugins (plug-in)
    environments (environment configuration)
    environment (environment variable)
    transactionManager (transaction manager)
    dataSource (data source)
    databaseIdProvider (database vendor identification)
    mappers (mapper)

2. Environment configuration (environments)

Mybatis can be configured to adapt to multiple environments

But remember: Although multiple environments can be configured, only one environment can be selected per SqlSessionFactory instance

Learn to use and configure multiple operating environments

The default transaction manager of Mybatis is JDBC, connection pool: POOLED

3. Properties

We can implement the reference configuration file through the properties attribute

These properties are externally configurable and dynamically replaceable. They can be configured in typical Java property files or passed through sub-elements of the properties element. 【db.properties】

write a configuration file

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username=root
password=123456

Introduced in the core file

    <!--引入外部配置文件-->
    <properties resource="db.properties"/>

Can import external files directly

You can add some attribute configurations to it

If two files have the same field, use the external configuration file first

4, type aliases (typeAliases)

A type alias is a short name set for a java type

The meaning of existence is only used to reduce the redundancy of the fully qualified name of the class.

<!--可以给实体类写别名-->
    <typeAliases>
        <typeAlias type="com.jiang.pojo.User" alias="User"></typeAlias>
    </typeAliases>

You can also specify a package name, Mybatis will search for the required Java Bean under the package name, for example: scan the package of the entity class, its default alias is the class name of this class, the first letter is lowercase

<!--可以给实体类写别名-->
    <typeAliases>
        <package name="com.jiang.pojo"/>
    </typeAliases>

It is suitable for fewer entity classes, use the first method

If there are many entity classes, it is recommended to use the second

The first type can be DIY aliases, but the second type cannot. If you have to change it, you need to add annotations to the entity class

@Alias("user")
public class User{}

5. Settings

This is an extremely important adjustment setting in Mybatis, which will change the runtime behavior of Mybatis

 

 

6. Other configuration

7. Mapper mappers

MapperRegistry: Register and bind our Mapper file;

Method 1: [recommended]

<!--每一个Mapper.xml都需要在MyBatis核心配置文件中注册-->
<mappers>
    <mapper resource="com/jiang/dao/UserMapper.xml"/>
</mappers>

Method 2: Use the class file to bind and register 

<!--每一个Mapper.xml都需要在MyBatis核心配置文件中注册-->
<mappers>
    <mapper resource="com.jiang.dao.UserMapper"/>
</mappers>

important point:

  • The interface and its Mapper configuration file must have the same name
  • The interface and its Mapper configuration file must be in the same package

Method 3: Use package scanning for injection


<mappers>
   <package name="com.jiang.dao"/>
</mappers>

 8. Life cycle and scope

 

Lifecycle and scoping are critical, because wrong usage can lead to very serious concurrency problems .

SqlSessionFactoryBuilder:

Once the SqlSessionFactory is created, it is no longer needed for
local variables

SqlSessionFactory:

To put it bluntly, it can be imagined as: once the database connection pool
SqlSessionFactory is created, it should always exist during the running of the application. There is no reason to discard it or recreate an instance.
So the best scope for SqlSessionFactory is application scope (ApplicationContext).
The easiest is to use the singleton pattern or the static singleton pattern .

SqlSession:

A request to connect to the connection pool

Instances of SqlSession are not thread-safe and therefore cannot be shared, so its best scope is request or method scope.

You need to close it quickly after use, otherwise resources will be occupied!

 Each Mapper here represents a specific business!


https://www.bilibili.com/video/BV1NE411Q7Nx?p=10&spm_id_from=pageDriver

Guess you like

Origin blog.csdn.net/qq_48108092/article/details/124134639