configuration
properties
These properties of the database can be externally configured and dynamically replaced. They can be configured in a typical Java property file or through
properties element. Specific official documents
Let's optimize our profile
The first step; Create a new db.properties in the resource directory
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8 username=root password=123456
Step 2: import the file into the properties configuration file
<configuration> <!--Import properties file--> <properties resource="db.properties"/> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
settings
settings = > View help documentation
Lazy loading
Log implementation
Cache on / off
An example of a fully configured settings element is as follows:
<settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25"/> <setting name="defaultFetchSize" value="100"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="mapUnderscoreToCamelCase" value="false"/> <setting name="localCacheScope" value="SESSION"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> </settings>
typeAliases
A type alias is a short name for a Java type. It is only related to XML configuration, and its significance is only to reduce the redundancy of class fully qualified names.
<!--Configure alias,Pay attention to the order--> <typeAliases> <typeAlias type="com.kuang.pojo.User" alias="User"/> </typeAliases>
When configured in this way, User can be used wherever com.xxx.pojo.User is used.
You can also specify a package name. MyBatis will search for the required Java beans under the package name, such as:
<typeAliases> <package name="com.xxx.pojo"/> </typeAliases>
Each Java Bean in the package com.xxx.pojo will use the initial lowercase unqualified of the bean without annotation
Class name as its alias.
If there is an annotation, the alias is its annotation value. See the following example:
@Alias("user") public class User { ... }
typeHandlers
Whether MyBatis sets a parameter in the PreparedStatement or takes a value from the result set, it will use the type processor to convert the obtained value into Java type in an appropriate way.
objectFactory (object factory)
Every time MyBatis creates a new instance of the result object, it uses an object factory instance to complete it.
The default object factory only needs to instantiate the target class, either through the default constructor or through the parametric constructor when the parameter mapping exists.
If you want to override the default behavior of an object factory, you can do so by creating your own object factory.
plugins
environments (environment configuration) environment (environment variable)
Configure multiple sets of running environments for MyBatis and map SQL to multiple different databases. You must specify one of them as the default running environment
(specified by default)
Child element node: environment
The dataSource element uses the standard JDBC data source interface to configure the resources of the JDBC connection object.
The data source must be configured.
There are three built-in data source types
type="[UNPOOLED|POOLED|JNDI]")
unpooled: the implementation of this data source only opens and closes the connection every time it is requested.
pooled: the implementation of this data source uses the concept of "pool" to organize JDBC connection objects, which is a popular way to make concurrent Web applications respond to requests quickly.
jndi: this data source is implemented to be used in containers such as Spring or application server, which can be centralized or external
Configure the data source, and then place a reference to the JNDI context.
Data sources also have many third-party implementations, such as dbcp, c3p0, druid, etc
Transaction manager
dataSource
databaseIdProvider (database vendor ID)
mappers
Mapper: define mapping SQL statement files
Now that the behavior of MyBatis and other elements have been configured, we are going to define the SQL mapping statement. But first we need to tell MyBatis where to find these statements. Java does not provide a good way to automatically find this, so the best way is to tell MyBatis where to find the mapping file. You can use resource references relative to the classpath, or fully qualified resource locators (including the URL of file: / /), or class and package names. Mapper is one of the core components in MyBatis. Before MyBatis 3, only xml mapper was supported, that is, all SQL statements must be configured in xml files. Starting from MyBatis 3, it also supports interface mapper, which allows the annotation and definition of SQL statements in the form of Java code, which is very concise.
<!-- Use resource references relative to Classpaths --> <mappers> <mapper resource="org/mybatis/builder/PostMapper.xml"/> </mappers> <!-- Use fully qualified resource locators( URL) --> <mappers> <mapper url="file:///var/mappers/AuthorMapper.xml"/> </mappers> <!-- Use the mapper interface to implement the fully qualified class name of the class The configuration file name and interface name should be consistent and located in the same directory --> <mappers> <mapper class="org.mybatis.builder.AuthorMapper"/> </mappers> <!-- Register all the mapper interface implementations in the package as mappers However, the configuration file name and interface name should be consistent and located in the same directory --> <mappers> <package name="org.mybatis.builder"/> </mappers>