[MyBatis] Core configuration file, the scope of the three major objects, the difference between #{} and ${}

1. environment environment:

  • Generally, an environment environment will correspond to a SqlSessionFactory object

  • sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"), "id of another environment" );

// 数据库环境配置在这个标签里
// 这里的default表示默认使用的环境
<environments default="">
    // 一般一个环境environment会对应一个SqlSessionFactory对象
    <environment id="">

    </environment>
    
    // 这是mybatis的另一个环境, 连接的数据库是另外一个数据库
    <environment id="">

    </environment>
</environments>

// @test
public static void main(String[] args) {
    SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

    // 获取默认的环境
    SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
    SqlSession sqlSession = sqlSessionFactory.openSession();
    
    // 通过环境id来使用指定的环境
    SqlSessionFactory sqlSessionFactory1 = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"),"另一个环境的id");
    SqlSession sqlSession1 = sqlSessionFactory1.openSession();
}

2. Transaction Manager:

  • The type in the transactionManager tag of the core configuration file can only select JDBC / MANAGED (case insensitive)

  • The function is to configure the transaction manager and specify how mybatis uses to manage transactions.

  • JDBC: Use native jdbc code to manage transactions . As follows:

conn.setAuotCommit(false);
...业务处理...
conn.commit();
  • MANAGED: mybatis is no longer responsible for transaction management , and will hand over the transaction to other containers for management.

Transaction接口, 有俩个实现类(JdbcTransaction/ManagedTransaction)

如果type="jdbc", 那么底层会实例化JDBCTransaction对象
如果type="managed", 那么底层会实例化ManagedTransaction对象

3. Analysis of data sources:

  • The dataSource in the dataSource tag in the core configuration file is called the data source

  • What is the role of dataSource, to provide the Connection object for the program

  • But anyone who provides a connection object to the program is called a data source

  • The data source is actually a set of specifications, and there is a set of specifications in the JDK: javax.sql.DataSource.

  • We can also write data source components ourselves, as long as we implement the javax.sql.DataSource interface and implement all the methods in the interface

  • The database connection pool provides connection objects, so the database connection pool is a data source

  • What are the common data source components (what are the common database connection pools): Alibaba's Druid connection pool, c3p0...

  • The type attribute can be used to specify the type of data source, which is to specify the specific method to use to obtain the Connection object

  • The type attribute has three values: unpooled, pooled, jndi

  • unpooled: Does not use database connection pooling technology, after each request, a new Connection object is created.

  • pooled: use the database connection pool implemented by mybatis itself

  • jndi: Integrate other third-party database connection pools

  • Different configurations require different attributes. Refer to the following figure:

4. Configuration and use of the properties tag:

4.1 Method 1:

  • Configure properties in the core configuration file

  • The Properties class is a Map collection, and both key and value are of type String

  • Note the use of ${}

// Properties类是一个Map集合, key和value都是String类型的
<properties>
     <property name="jdbc.driver" value="..."/>
     <property name="jdbc.url" value="..."/>
     <property name="jdbc.username" value="..."/>
     <property name="jdbc.password" value="..."/>
</properties>


<environment id="development">
    <transactionManager type="JDBC"/>
    <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>

4.2 Method 2:

  • Write a file with properties as the suffix

  • this way is better

// jdbc.properties文件
jdbc.driver=...
jdbc.url=...
jdbc.username=...
jdbc.password=...


// mybatis-config.xml
<properties resource=""jdbc.properties"/>


<environment id="development">
    <transactionManager type="JDBC"/>
    <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>

5. The scope of the three major objects:

5.1 SqlSessionFactoryBuilder:

  • This class can be instantiated, once the SqlSessionFactory is created it is no longer needed

  • So the role of SqlSessionFactoryBuilder is to create SqlSessionFactory

5.2 SqlSessionFactory:

  • Once created, the SqlSessionFactory should exist for as long as the application is running

  • Do not repeatedly create SqlSessionFactory objects, it is recommended to place them in static code blocks

  • A database corresponds to a SqlSessionFactory object

  • The core configuration file of Mybatis does not need to be parsed repeatedly, it only needs to be parsed once when the server starts

5.3 SqlSession:

  • A thread corresponds to a SqlSession object , because a thread corresponds to a transaction

  • Instances of SqlSession are not thread-safe

6. The difference between #{} and ${}:

  • #{}Placeholder

1. 先进行sql语句的编译, 再给占位符传值

2. 底层是PreparedStatement实现

3. 可以防止sql注入, 不知道用户输入的信息是啥, 所以优先使用#{}.
  • ${} placeholder

1. 先进行sql语句拼接, 然后再编译sql语句

2. 底层是Statement实现 

3. 存在sql注入现象, 只有在需要进行sql语句关键字拼接的情况下才会用到

4. 如果传入的是关键字(asc/desc), 那就需要使用${}才行, 因为#{}是以值的形式放到sql语句中

5. 拼接表名的时候需要使用${}

Guess you like

Origin blog.csdn.net/qq_68993495/article/details/128834471