MyBatis profile
Master profile
There are two categories of mybatis configuration files: 1. Main configuration file 2.mapper file
mybatis.xml used in the previous project is the main configuration file.
Master configuration file: provides global settings for mybatis, including log, data source, mapper file location, etc.
Mapper file: write sql statements, one table and one mapper file.
<?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> <!--Set log--> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings> <!--alias declarations --> <typeAliases> <!-- First syntax format: type: java Fully qualified name of the type (custom type) alias: Custom alias Advantage: alias can be customized Disadvantages: each type must be defined separately --> <typeAlias type="com.lln.vo.Student" alias="stu"/> <!-- Second syntax format: name: Package name, mybatis All class names in this package will be used as aliases (case insensitive) --> <package name="com.lln.vo"/> </typeAliases> <!--to configure mybatis environment--> <environments default="mysql"> <!--id:Name of the data source--> <environment id="mysql"> <!--Configuring transaction types: Using JDBC Transaction (use) Connection (commit and rollback of)--> <transactionManager type="JDBC"/> <!--data source dataSource: Create database Connection object type: POOLED Connection pool using database--> <dataSource type="POOLED"> <!--Four elements of connecting to a database--> <!--driver: Driven content--> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/lln/dao/StudentDao.xml"/> </mappers> </configuration>
Head use constraint file
<?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">
Root element
<configuration> </configuration>
settings section
settings is the global setting of mybatis, which affects the operation of the whole mybatis. This setting generally uses the default value.
<settings> <setting name="logImpl" value="STDOUT_LOGGING"/> </settings>
typeAliases declare aliases
<!--alias declarations --> <typeAliases> <!-- First syntax format: type: java Fully qualified name of the type (custom type) alias: Custom alias Advantage: alias can be customized Disadvantages: each type must be defined separately --> <typeAlias type="com.lln.vo.Student" alias="stu"/> <!-- Second syntax format: name: Package name, mybatis All class names in this package will be used as aliases (case insensitive) --> <package name="com.lln.vo"/> </typeAliases>
Configuration environment
Environments: environment tag, where multiple environments can be configured
Default: attribute indicating the database environment to which mybatis connects by default
environment: represents the connection information of a database.
Attribute id: user defined environment id, unique value.
Transaction manager: transaction manager
Attribute type: indicates the type of transaction manager.
type attribute value:
(1) JDBC: using the Connection object, mybatis completes the transaction processing by itself.
(2) MANAGED: MANAGED, which means that the transaction processing is handed over to the container implementation.
dataSource: the data source, the created Connection object, and the Connection database.
Attribute: type data source type
type attribute value:
(1) POOLED and mybatis will create a PooledDataSource class in memory to manage the Connection pool used by multiple Connection objects.
(2) UNPOOLED, not applicable to Connection pool. mybatis creates an UnPooledDataSource class. Each time an sql statement is executed, first create a Connection object, then execute the sql statement, and finally close the Connection.
(3) JNDI, java Naming and directory service. A data source implemented using JNDI.
<!--to configure mybatis environment--> <environments default="mysql"> <!--id:Name of the data source--> <environment id="mysql"> <!--Configuring transaction types: Using JDBC Transaction (use) Connection (commit and rollback of)--> <transactionManager type="JDBC"/> <!--data source dataSource: Create database Connection object type: POOLED Connection pool using database--> <dataSource type="POOLED"> <!--Four elements of connecting to a database--> <!--driver: Driven content--> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments>
dataSource configuration
In the MyBatis.xml main configuration file, configure the dataSource:
During initialization, MyBatis creates a data source of the corresponding type according to the type attribute of the,
Namely:
type = "POOLED": MyBatis will create a PooledDataSource instance
type = "UNPOOLED": MyBatis will create an UnpooledDataSource instance
type = "JNDI": MyBatis will find the DataSource instance from the JNDI service, and then return it for use
Database properties profile
In order to facilitate the management of database connection, the four element data of DB connection is generally stored in a special attribute file. The MyBatis main configuration file needs to read this data from this property file.
Create a jdbc.properties file in the resources directory with a custom file name.
Modify the main configuration file, and add the file start location:
Use key value:
affair
(1) By default, transactions need to be submitted manually
The Mybatis framework encapsulates JDBC, so the transaction control method of the Mybatis framework also uses the commit(), rollback(), of the Connection object of JDBC
The setAutoCommit() method of the Connection object to set the transaction submission method. Automatic submission and manual submission
<transactionManager type="JDBC"/>
This label specifies the transaction manager used by MyBatis. MyBatis supports two transaction manager types: JDBC and MANAGED.
JDBC: transaction management mechanism using JDBC. That is, submit through the commit() method of Connection and roll back through the rollback() method. However, by default, MyBatis turns off the automatic submission function and changes to manual submission.
MANAGED: the entire life cycle of a transaction is MANAGED by a container (such as the Spring container).
(2) Auto commit transaction
Set the method of automatic submission. openSession() of factory is divided into parameter and no parameter.
//Create a method to get the SqlSession object public static SqlSession getSqlSession(){ SqlSession session = null; if(factory != null){ session = factory.openSession(); } return session; }
If the parameter is true, you can modify the getSqlSession() method of MyBatisUtil by using automatic submission.
session = factory.openSession(true);
Then execute the insert operation without executing session.commit(), and the transaction is automatically committed
mapper file
The first way:
Use mapper to specify the location of other mapper files, and find files from the classpath path relative to the resources of the classpath.
Advantages: the file is clear, the loaded file is clear, and the location of the file is flexible.
Disadvantages: when there are many files, the amount of code is large and the management is difficult.
<mappers> <mapper resource="com/lln/dao/StudentDao.xml"/> </mappers>
The second way:
Specify all Dao interfaces under the package.
Name: package name, the package name where the mapper file is located.
Features: load all mapper files in this package at one time.
<package name="com.xxxx.dao"/>
Usage requirements: this method requires that the Dao interface name and the mapper mapping file name are the same and in the same directory.