Eighty-five, MyBatis configuration file

Article directory

main configuration file

dataSource tag

dataSource type

dataSource configuration

affairs

Use the database properties configuration file

typeAliases

mappers


main configuration file

mybatis.xml used in the previous project is the main configuration file.

Main Profile Features:

1, xml file, you need to use the constraint file in the header

<?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">

2. The root element, <configuration>

3. The main contents include:

  • define alias
  • data source
  • mapper file

dataSource tag

To access the database in Mybatis, connection pooling technology is available, but it uses its own connection pooling technology. In the mybatis.xml configuration file of Mybatis, the configuration of the connection pool in Mybatis is realized through <dataSource type="pooled">.

dataSource type

The above figure shows that Mybatis divides data sources into three categories:

  1. UNPOOLED data source that does not use connection pooling
  2. POOLED data source using connection pool
  3. JNDI Data sources implemented using JNDI

Among them, UNPOOLED and POOLED data sources implement the javax.sq.DataSource interface, and JNDI is different from the previous two implementations. Just understand.

dataSource configuration

In the MyBatis.xml main configuration file, configure the dataSource:

<dataSource type="POOLED">
 <!--连接数据库的四个要素--> 
 <property name="driver" value="com.mysql.jdbc.Driver"/>
 <property name="url" 
value="jdbc:mysql://localhost:3306/ssm?charset=utf-8"/>
 <property name="username" value="root"/>
 <property name="password" value="123456"/>
</dataSource> 

When MyBatis is initialized, it creates the corresponding type of data source DataSource according to the type attribute, namely:

  • type=”POOLED”: MyBatis will create a PooledDataSource instance
  • type=”UNPOOLED” : MyBatis will create an instance of UnpooledDataSource
  • type=”JNDI”: MyBatis will look up the DataSource instance from the JNDI service, and then return to use

affairs

(1) By default, transactions need to be submitted manually

The Mybatis framework is an encapsulation of JDBC, so the transaction control method of the Mybatis framework itself also uses the commit() and rollback() of the Connection object of JDBC.

The setAutoCommit() method of the Connection object sets the transaction commit method. automatic submission and manual submission,

<transactionManager type="JDBC"/>

This tag is used to specify the transaction manager used by MyBatis. MyBatis supports two transaction manager types: JDBC and MANAGED.

JDBC: Transaction management mechanism using JDBC. That is, it is submitted through the commit() method of Connection and rolled back through the rollback() method. But by default, MyBatis turns off the automatic submission function and changes it to manual submission. That is, the program needs to explicitly commit or roll back the transaction. It can be seen from the output information of the log.

 MANAGED: The entire lifecycle of a transaction is managed by the container (eg Spring container).

(2) Automatically commit transactions

To set the method of automatic submission, the openSession() of the factory is divided into parameters with and without parameters.

 If the parameter is true, the getSqlSession() method of MyBatisUtil can be modified by using automatic submission.

session = factory.openSession(true);
再执行 insert 操作,无需执行 session.commit(),事务是自动提交的

Use the database properties configuration file

In order to facilitate the management of database connections, the four elements of DB connection data are generally stored in a special attribute file. The MyBatis main configuration file needs to read this data from this properties file. step:

(1) In the classpath path, create a properties file

Create a jdbc.properties file in the resources directory with a custom file name:

 (2) Use the properties tag

Modify the main configuration file and add the beginning of the file:

 (3) Use the key to specify the value

<dataSource type="POOLED">
 <!--使用 properties 文件: 语法 ${key}--> 
 <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>

typeAliases

Mybatis supports default aliases, we can also use custom aliases to develop, mainly used in <select resultType="alias"> 

The mybatis.xml main configuration file defines aliases:

<typeAliases>
 <!-- 
 定义单个类型的别名 
 type:类型的全限定名称 
 alias:自定义别名 
 --> 
 <typeAlias type="com.bjpowernode.domain.Student" alias="mystudent"/>
 <!-- 
 批量定义别名,扫描整个包下的类,别名为类名(首字母大写或小写都
可以) 
 name:包名 
 --> 
 <package name="com.bjpowernode.domain"/>
 <package name="...其他包"/>
</typeAliases>

mapper.xml file, using aliases for types:

<select id="selectStudents" resultType="mystudent"> 
 select id,name,email,age from student
</select> 

mappers

(1)<mapper resource=" " />

Find files from classpath using classpath-relative resources

E.g:

<mapper resource="com/bjpowernode/dao/StudentDao.xml" /> 

(2)<package name=""/>

All Dao interfaces under the specified package

E.g:

<package name="com.bjpowernode.dao"/>

Note: This method requires that the Dao interface name and the mapper mapping file have the same name and are in the same directory.


Guess you like

Origin blog.csdn.net/m0_54925305/article/details/123693538