- Source: official document-XML configuration
- The contents of configuration files are in a certain order. You can default but cannot jump the queue. The order is as follows:
Configuration properties Settings typeAliases typeHandlers objectFactory plugins DataSource databaseIdProvider Mappers mappersCopy the code
1. Properties
- These properties can be configured externally and can be dynamically replaced. You can configure these properties either in a typical Java properties file or in a child element of the Properties element
- First of all in
resources
Create a new file in the folderconfig.properties
And writes the properties that need to be configured
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis? serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
username=root
password=root
Copy the code
- in
mybatis-config.xml
Of the fileconfiguration
addproperties
Structure. If there is nothing to add,properties
Section to add a single-line structure. throughThe ${}
useproperties
The content of the
<configuration>
<! Read the external configuration file -->
<properties resource="config.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<! -- SQL driver, need to change according to actual situation -->
<property name="driver" value="${driver}"/>
<! SQL > select * from 'SQL';
<property name="url" value="${url}"/>
<! -- Username and password -->
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<! -- Sets the mapper to be scanned, separated by '/'! -->
<mappers>
<mapper resource="com/du/mybatis/dao/UserMapper.xml"/>
</mappers>
</configuration>
Copy the code
Additional properties can also be added to properties
<properties resource="config.properties">
<property name="username" value="root"/>
<property name="password" value="root"/>
</properties>
Copy the code
- Note: When the external profile conflicts with the property content, the external profile prevails
- Is also available in the SqlSessionFactoryBuilder is. The build () method is introduced into property values
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, props); // props is the properties file read in
/ /... Or...
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environment, props);
Copy the code
typeAliases
- A type alias sets an abbreviated name for a Java type. It is only used for XML configuration and is intended to reduce redundant fully qualified class name writing
<! -- Set alias -->
<! -- type is the original fully qualified class name and alias is the subsequent alias, using the Bean's first lowercase unqualified class name as its alias -->
<typeAliases>
<typeAlias type="com.du.mybatis.pojo.User" alias="user"/>
</typeAliases>
Copy the code
Type is the original fully qualified class name and alias is a subsequent alias that uses the Bean’s lowercase, unqualified class name as its alias
<! -- Set alias -->
<typeAliases>
<! Scan all beans in the package and alias all class names to lowercase unqualified class names -->
<package name="com.du.mybatis.pojo"/>
</typeAliases>
Copy the code
Scan all beans in the package and alias all class names to lowercase, unqualified class names. That is, the alias of com.du.mybatis. Pojo. User is User
- You can put an annotation on the bean to indicate its alias. When scanning a package, use the alias of the annotation (but I’ve tried that doesn’t work…).
@Alias("author")
public class Author {
...
}
Copy the code
- Built-in type aliases for common Java types
Iii. Environment Configuration
- You can configure multiple environments and select one of them in default.
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<! -- SQL driver, need to change according to actual situation -->
<property name="driver" value="${driver}"/>
<! - the url of the SQL - >
<property name="url" value="${url}"/>
<! -- Username and password -->
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<! -- SQL driver, need to change according to actual situation -->
<property name="driver" value="${driver}"/>
<! - the url of the SQL - >
<property name="url" value="${url}"/>
<! -- Username and password -->
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
Copy the code
You can choose between development and test. Currently, Development is selected
3.1 transactionManager
- In MyBatis there are two types of transaction manager (i.e., type = “[JDBC | MANAGED]”), generally choose the default JDBC
- If you are using Spring + MyBatis, there is no need to configure the transaction manager because the Spring module overrides the previous configuration using its own manager. Neither of these transaction manager types requires any properties to be set. They are really type aliases; in other words, you can replace them with the fully qualified name or type alias of the class implemented by the TransactionFactory interface
3.2 dataSource
- There are three kinds of built-in data types (type = “[UNPOOLED | POOLED | JNDI]”)
- UNPOOLED: The implementation of this data source opens and closes connections on each request
- POOLED: This data source implementation leverages the concept of “pooling” to organize JDBC connection objects, avoiding the initialization and authentication time required to create new connection instances. This is a popular way to allow concurrent Web applications to respond quickly to requests
- JNDI: This data source implementation is intended for use in a container such as an EJB or application server, which can centrally or externally configure the data source and then place a data source reference to the JNDI context
Mappers
- There are several ways to define SQL mapping statements
- Using file paths
<! -- Use a resource reference relative to the classpath -->
<mappers>
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
<mapper resource="org/mybatis/builder/BlogMapper.xml"/>
<mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>
Copy the code
- Find files with the same class name in the package by the class name of the interface
<! Implement the fully qualified class name of the class using the mapper interface
<mappers>
<mapper class="org.mybatis.builder.AuthorMapper"/>
<mapper class="org.mybatis.builder.BlogMapper"/>
<mapper class="org.mybatis.builder.PostMapper"/>
</mappers>
Copy the code
The XML file must be in the same folder as the interface file and have the same name
- Scans all mapper in the specified packet
<! Register all mapper interface implementations as mapper
<mappers>
<package name="org.mybatis.builder"/>
</mappers>
Copy the code
The conditions are the same as above