Mybatis configuration resolution (core configuration file)

4. Configuration resolution

4.1. Core configuration file

We only need to learn the key points, and the rest as understanding

4.2. Environment configuration

Mybatis can be configured to adapt to a variety of environments

However, remember that although multiple environments can be configured, only one environment can be selected for each SqlSessionFactory instance

Learn to use and configure multiple operating environments!

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

4.3. properties

We can import external configuration files through properties. Examples are as follows:

Write dB properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=root
<configuration>
    <!--properties Must be placed first in the configuration file-->
    <!--Import external profile-->
    <properties resource="db.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!--You can use it directly here-->
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/kuang/dao/UserMapper.xml"/>
    </mappers>
</configuration>

4.4 type aliases

First:

<!--You can alias an entity class-->
<typeAliases>
    <typeAlias type="com.kuang.pojo.User" alias="User"/>
</typeAliases>

Through its alias, our return value type and parameter type do not need to be written so long

<select id="getUserList" resultType="User">
    select * from mybatis.user
</select>

Second:

You can also specify a package name. Mybatis will search for the required JavaBeans under the package name, such as:

Scan the package of the entity class, and its default is the class name of the class (initial lowercase)

<!--You can alias an entity class-->
<typeAliases>
    <package name="com.kuang.pojo"/>
</typeAliases>xml

Therefore, by directly specifying a package, all classes under the package will have their own aliases, which is faster

If we do not want to use lowercase as his alias when specifying the package (i.e. using the second method), we can continue to alias the class through annotation, for example:

@Alias("hello")
public class User {

So there are two ways to alias

If there are very few entity classes, use the first method

If there are many entity classes, the second method is recommended

4.5 setting

These are very important tuning settings in mybatis, which will change the runtime behavior of mybatis

4.6 other configurations

  • Type handlers

  • Object factory

  • plugins

4.7 mappers

MapperRegistry: register our Mapper files

  • Method 1: [no error] [recommended] [the other two are good]
<mappers>
    <mapper resource="com/kuang/dao/UserMapper.xml"/>
</mappers>
  • Method 2: (if there are certain requirements, use class file binding for registration)
<mappers>
    <mapper class="com.kuang.dao.UserMapper"/>
</mappers>

​ Note:

​ Interface and its Mapper configuration file must have the same name

​ Interface and its Mapper configuration file must be under the same package

  • Method 3: use scan package for injection binding
<mappers>
    <package name="com.kuang.dao"/>
</mappers>

​ Note:

​ Interface and its Mapper configuration file must have the same name

​ Interface and its Mapper configuration file must be under the same package

4.8 life cycle and scope

Lifecycle and scope are critical because incorrect use can lead to very serious concurrency problems

SqlSessionFactoryBuilder:

  • Once SqlSessionFactory is created, it is not needed
  • local variable

SqlSessionFactory:

  • To put it bluntly, it can be imagined as: database connection pool
  • Once SqlSessionFactory is created, it should always exist during the operation of the application. There is no reason to lose other instances or re create another instance
  • Therefore, the best scope of SqlSessionFactory is the application scope
  • The simplest is to use singleton mode or static singleton mode

SqlSession

  • A request to connect to the connection pool
  • The instance of sqlsession is not thread safe, so it cannot be shared, so its best scope is the request or method scope
  • You need to close it immediately after it is used up, otherwise the resources will be occupied

Each Mapper in this represents a specific business.

Keywords: Mybatis

Added by ether on Tue, 11 Jan 2022 11:13:35 +0200