MyBatis configuration file
Global configuration file details
In mybatis project, there is always a mybatis-config. XML configuration file, this configuration file is mybatis global configuration file, used to carry out myBatis related global configuration, the configuration takes effect under any operation. Below we want to do a detailed explanation of the attributes, convenient for you in the subsequent use of more skilled.
The MyBatis profile contains Settings and properties that will significantly affect MyBatis behavior. The top-level structure of the configuration document is as follows
-
Configuration
-
Properties
-
Settings
-
typeAliases
-
typeHandlers
-
objectFactory
-
Plugins
-
Environments
- Environment (environment variable)
- transactionManager
- A dataSource
- DatabaseIdProvider (Database vendor Identity)
- Mappers (mapper)
-
mybatis-config.xml
<! DOCTYPEconfiguration
PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<! -- Import external configuration files, similar to property-placeholder resources in Spring: import url from classpath: import URL from disk path or network path -->
<properties resource="db.properties"></properties>
<! -- Used to control the behavior of mybatis runtime.
<settings>
<! Set column name mapping to hump -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<! TypeAliases indicate aliases for the entity class that we reference. By default, we need to write the fully qualified name of the class. You can omit case and use the alias attribute to indicate the class alias.
<typeAliases>
<! -- <typeAlias type="com.mybatis.bean.Emp" alias="Emp"></typeAlias>-->
<! If you need to refer to more than one class, aliasing each class is cumbersome, so you can specify the corresponding package name, which is the default class name.
<package name="com.mybatis.bean"/>
</typeAliases>
<! -- In the actual development process, we may be divided into development environment, production environment, test environment and so on. The configuration of each environment can be different, which is used to represent the detailed configuration of different environment. Each environment requires a transaction manager and data source configuration. In the subsequent project development, we almost use the data source and transaction manager configured in Spring to configure. There is no need to study here.
<! --default: select the desired environment -->
<environments default="development">
<! --id: indicates the name of the different environment -->
<environment id="development">
<transactionManager type="JDBC"/>
<! -- Configure database connection -->
<dataSource type="POOLED">
<! -- use ${} to introduce external variables -->
<property name="driver" value="${driverClassname}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<! SQL > select * from database where SQL statement can be executed; select * from database where SQL statement can be executed;
<databaseIdProvider type="DB_VENDOR">
<property name="MySQL" value="mysql"/>
<property name="SQL Server" value="sqlserver"/>
<property name="Oracle" value="orcl"/>
</databaseIdProvider>
<! Use mappers to map SQL files -->
<mappers>
<! Class: the full class name of the interface directly. You can put the XML file in the same directory as the DAO and set the same file name. SQL mapping files can be found on disk or network path. Resource: SQL mapping files can be found on class path.
<! -- <mapper resource="EmpDao.xml"/> <mapper resource="UserDao.xml"/> <mapper class="com.mybatis.dao.EmpDaoAnnotation"></mapper>-->
<! - when containing multiple configuration file or configuration class, you can use the batch register function, also is the introduction of the corresponding package, rather than a specific configuration file or class But it is important to note that 1, if you use a configuration file, in the form of the configuration file must be with dao class put together, so as to find the corresponding configuration file. For maven projects, you also need to add the following configuration, <build> <resources> <resource> <directory> SRC /main/ Java </directory> <includes> <include> </include> </includes> </resources> </resources> </build>
<package name="com.mybatis.dao"/>
</mappers>
</configuration>
Copy the code
Mybatis SQL mapping file details
The real power of MyBatis lies in its statement mapping, which is its magic. Because of its exceptional power, the MAPper’s XML file is relatively simple. If you compare it to JDBC code with the same functionality, you’ll immediately see that nearly 95% of the code is saved. MyBatis aims to reduce usage costs and allow users to focus more on SQL code.
The SQL mapping file has only a few top-level elements (listed in the order they should be defined) :
- Cache – The cache configuration for this namespace.
- Cache-ref – References the cache configuration of other namespaces.
- ResultMap – describes how to load objects from a database result set, and is the most complex and powerful element.
- ParameterMap – Old-fashioned style parameter mapping. This element is deprecated and may be removed in the future! Use inline parameter mapping. This element is not covered in the documentation.
- SQL – reusable block of statements that can be referenced by other statements.
- Insert – Mapping insert statements.
- Update – Mapping update statement.
- Delete – Mapping delete statement.
- Select – Mapping query statement.
There are a number of attributes that can be added to each top-level element tag, so let’s take a closer look at the configuration.
Insert, update, delete elements
attribute | describe |
---|---|
id | A unique identifier in the namespace that can be used to refer to this statement. |
parameterType | The class-fully qualified name or alias that will be passed for the argument to this statement. This property is optional because MyBatis can infer the parameters of a specific incoming statement through the TypeHandler, which defaults to unset. |
parameterMap | Deprecated property used to reference an external parameterMap. Use inline parameter mapping and the parameterType attribute. |
flushCache | Setting this to true causes the local and secondary caches to be cleared whenever a statement is called. The default is true (for INSERT, UPDATE, and DELETE statements). |
timeout | This setting is the number of seconds the driver waits for the database to return the result of the request before throwing an exception. The default is unset (database driven). |
statementType | The value can be STATEMENT, PREPARED or CALLABLE. This will make MyBatis use Statement, PreparedStatement, or CallableStatement, respectively. The default value is PREPARED. |
useGeneratedKeys | (for INSERTS and Updates only) This will cause MyBatis to use JDBC’s getGeneratedKeys method to retrieve primary keys generated internally by the database (e.g. Auto-increment fields for relational database management systems like MySQL and SQL Server), default: false. |
keyProperty | (for INSERT and UPDATE only) Specifies a property that uniquely identifies the object. MyBatis sets its value using the return value of getGeneratedKeys or the selectKey child element of the INSERT statement. Default: unset. If more than one column is generated, you can separate multiple attribute names with commas. |
keyColumn | (for INSERT and UPDATE only) Sets the name of the column in the table that generates the key value. In some databases (like PostgreSQL), this is mandatory when the primary key column is not the first column in the table. If more than one column is generated, you can separate multiple attribute names with commas. |
databaseId | If databaseIdProvider is configured, MyBatis will load all statements that do not have a databaseId or match the current databaseId. If both tagged and untagged statements are present, untagged statements are ignored. |