Analysis of the core configuration file of Mybatis

table of Contents

Core configuration file

Environment configuration (environments)

Properties

Type aliases (typeAliases)

Settings

Mappers

The principle and use of SqlSession


 

Core configuration file

 

mybatis-config.xml, the following only talk about commonly used

 

 

Environment configuration (environments)

 

Mybatis can be configured to adapt to a variety of environments, but remember that only one of them (the one with deafult) can be used when using it

The default transaction manager in Mybatis is JDBC, and the connection pool is POOLED

In addition to JDBC, the transaction manager also has MANAGED (for traditional EJB projects), of course, the connection pool can also be set to UNPOOLED

 

 

Properties


 

We can refer to the configuration file through properties, these properties are externally configurable and can be dynamically replaced .

One solution is to define a db.properties configuration file separately and then reference it in the project default configuration file

Note: When referencing, xml specifies the order of the tags . The <properties> tag must be immediately below the configuration tag, otherwise an error will be reported.

 

It can also exist in the form of <properties> tag in the default xml configuration file of the project ,

If the username and password are written in the external configuration file, and you write the username and password again in the main configuration file in the form of a label, mybatis will preferentially use the information in the external configuration file .

 

 

Type aliases (typeAliases)

 

In the mapper.xml file, each of our sql must be marked entity class path name, if this name is too long, this is not too troublesome

 We have three ways to simplify type aliases

  • 1. Alias ​​entity classes in the main configuration file

Note that the writing position must be written after the <properties> tag and <settings> tag, otherwise an error will be reported 

 

  • 2. You can also write only the package name in the typeAliases tag, and the specific class name is written in the mapper

  • 3. On the basis of the second scheme, by adding Aliases annotation to the entity class

The premise is that you must specify the scan package in the configuration file, and then you can name it at will when you annotate the entity class.

 

 

Settings

 

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

 

 

Mappers

 

The mapper plays the role of registering our custom mapper.xml in the main configuration file for mybatis to recognize. 

There are several ways, the first one is recommended

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

 Use this method: the interface and his Mapper configuration file must have the same name! The interface and his Mapper configuration file must be in the same package!

<!--每一个Mapper.XML都需要在Mybatis核心配置文件中注册!-->
<mappers>
    <mapper class="com.kuang.dao.UserMapper"/>
</mappers>
  • package
Use this method: the interface and his Mapper configuration file must have the same name! The interface and his Mapper configuration file must be in the same package!
 
<!--每一个Mapper.XML都需要在Mybatis核心配置文件中注册!-->
<mappers>
    <package name="com.kuang.dao"/>
</mappers>
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==uploading.4e448015.gifUnsuccessful transfer and re-upload cancel wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

 

 

The principle and use of SqlSession

 

mybatis perform graphic wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==uploading.4e448015.gifdump failed to re-upload canceledwAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw ==

 

The core of this is Builder, Factory, and SqlSession. Let's explain

  • SqlSessionFactoryBuilder

Is used to create SqlSessionFactory, so the scope of this thing is local variables

  • SqlSessionFactory

It can be understood as a database connection pool. Once this factory is created, it always exists during the running of the application, so the best scope is the application scope, which is used by singleton mode or static singleton mode.

  • SqlSession

One request in the connection pool, because of the concurrency of SqlSession, the thread is not safe and cannot be shared, so the best scope is the request or method scope, which is closed immediately after use to prevent concurrency problems in resource occupation

 

You can compare and understand ~

 

 

Published 568 original articles · Like 180 · Visits 180,000+

Guess you like

Origin blog.csdn.net/Delicious_Life/article/details/105649744