Parse Mybaits core configuration file properties

Table of contents

1.environment

2.transactionManager

3.dataSource

4.peoperties

5.mapper


First look at the mybatis core configuration file code
 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <properties resource="jdbc.properties"></properties>
    <environments default="mysql">
        <environment id="mysql">
            <!--配置事务的类型,使用本地事务策略-->
            <transactionManager type="JDBC"></transactionManager>
            <!--是否使用连接池 POOLED表示使用链接池,UNPOOLED表示不使用连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="CarMapper.xml"></mapper>
    </mappers>
</configuration>

1.environment


A database corresponds to an environment
<environments default="mysql"> indicates the default development environment   
// A database corresponds to a SqlSessionFactory object
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
// Two databases correspond to two SqlSessionFactory objects, and so on  
SqlSessionFactory sqlSessionFactory1 = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"), "dev");


2.transactionManager

<transactionManager type="JDBC"></transactionManager>
indicates that the transaction manager is JDBC, and transactions need to be submitted manually.
<transactionManager type="MANAGED"></transactionManager>
indicates that the transaction is managed by the container. If there is no container support, when When no container management is found, there is no transaction. It is to submit a statement and submit it once automatically.

3.dataSource


<dataSource type="UNPOOLED">
UNPOOLED will not use the connection pool, and will create a new JDBC connection object every time. POOLED will use a database connection pool.
When <dataSource type="POOLED">, it has some attributes (not only these)

poolMaximumActiveConnections: The maximum number of active connections. default value 10

poolMaximumIdleConnections: The maximum number of idle connections. default value 5

poolMaximumCheckoutTime: The time to forcefully return to the pool. The default value is 20 seconds.

poolTimeToWait: When an idle connection cannot be obtained, the log is printed every 20 seconds to avoid stupidity due to incorrect code configuration. (duration is configurable)
 

4.peoperties

The information about mybatis connecting to the database can be configured in a separate property file, usually named jdbc.properties, and
the database connection information is written in this file

 Reference jdbc.properties in the main configuration file

5.mapper


The mapper tag is used to specify the path of the SQL mapping file, including a variety of specifying methods. Here we mainly look at two of them: the
first one: resource, which is loaded from the root path of the class (commonly used)

<mappers>
  <mapper resource="CarMapper.xml"/>
</mappers>
The second type: url, loaded from the specified url location

<mappers>
  <mapper url="file:///d:/CarMapper.xml"/>
</mappers>

Of course, there are many configuration methods, including the method of package scanning, which will be put in a later article.

Guess you like

Origin blog.csdn.net/weixin_53818758/article/details/130377007