[Mybatis] explain the core configuration file

Keywords: Java Database Maven Mybatis Back-end

🌈 I'm lucky to meet you. I'm Xiao Xiang. Have a big factory dream, work hard! Come on, stranger! 🌈
☁️ This article is the notes and experience of learning Mybatis from the crazy God of station B. if it is useful to you, thank you for your praise. The article inevitably has shortcomings. Please point out that I will correct it in time!

This part of the code is located at: mybatis-02-configFile · xcy. Xiaoxiang / Web mybatis - Code cloud - Open Source China (gitee.com)

1. mybatis-config.xml

The XML core configuration file contains the core configuration and global configuration of Mybatis. include:

  • settings tab: global configuration tab, which is used to turn on or off some functions in mybatis

  • typeAliases tag: type alias, which is intended to reduce redundant writing of fully qualified class names.

  • environments tag: the environment configuration tag to obtain the data source (driver, url, username, password, etc.) of the database connection instance.

  • mappers tag: a mapper that tells mybatis where to find SQL mapping statements. Each * Mapper.xml needs to be registered in the mybatis core configuration file.

  • Properties tag: a property tag used to import external configuration files or custom properties.

  • plugins

  • typeHandlers

  • objectFactory (object factory)

  • databaseIdProvider (database vendor ID)

The default transaction manager of mybatis is JDBC, and the default data source is POOLED.

2. Environment configuration

<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}"/>
  • Environment: MyBatis can configure multiple environments by configuring multiple environment tags, and specify the default environment by default.

    • Although multiple environments can be configured, only one environment can be selected for each SqlSessionFactory instance.
    • ID: the ID of the environment. You can specify the default environment through the default attribute of the environments tab.
  • Transaction manager: transaction manager. There are two types of transaction managers (type attribute) in MyBatis

    • JDBC: this configuration directly uses JDBC submission and rollback facilities, which is also the default configuration of mybatis.
    • MANAGED: this configuration does little.
  • dataSource: data source. The dataSource element uses the standard JDBC data source interface to configure the resources of the JDBC connection object. There are three built-in data source types (that is, type="[UNPOOLED|POOLED|JNDI]").

    • UNPOOLED: the implementation of this data source will open and close the connection every time it is requested.
    • POOLED: the implementation of this data source uses the concept of "pool" to organize JDBC connection objects, avoiding the necessary initialization and authentication time when creating new connection instances. This processing method is very popular and can enable concurrent Web applications to respond to requests quickly. This is also the default configuration for mybatis.
    • JNDI: this data source implementation is to be used in containers such as EJB or application server. The container can configure the data source centrally or externally, and then place a data source reference of JNDI context.
  • driver – the fully qualified name of a JDBC driven Java class
  • url – the JDBC URL address of the database
  • username – the user name to log in to the database
  • Password – the password to log in to the database
  • defaultTransactionIsolationLevel – the default connection transaction isolation level
  • defaultNetworkTimeout – the default network timeout in milliseconds to wait for the database operation to complete

3. properties

  • Properties: the properties tag can be used to introduce external configurations. The most typical is the introduction of the db.properties file. Then get the corresponding value through ${}.




<!--Import external profile-->
<properties resource="db.properties"/>

<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <!--                Configuration information of connection data-->
            <property name="driver" value="${Driver}"/>
            <property name="url" value="${Url}"/>
            <property name="username" value="${User}"/>
            <property name="password" value="${Pwd}"/>

In addition, you can configure data inside the properties tab.

<properties resource="db.properties">
     <property name="Driver" value="123456"/>
     <property name="User" value="root"/>

It should be noted that these two methods have a certain priority: direct assignment > external profile > internal profile.

4. Type aliases

In order to reduce the redundancy of fully qualified names, mybatis provides a method to set aliases: < typealiases >. The configured alias can be used anywhere.

    <typeAlias type="top.xcyxiaoxiang.pojo.User" alias="User"/>

When using, you can directly use the alias instead of the fully qualified name.

<!--    <update id="updateUser" parameterType="top.xcyxiaoxiang.pojo.User" >-->
<update id="updateUser" parameterType="User" >
    update users set name=#{name},pwd=#{pwd} where id=#{id}

You can also specify a package name. MyBatis will search for the required Java beans under the package name. In the absence of annotations, the initial lowercase unqualified class name of the Bean will be used as its alias. For example, the alias of top.xcyxiaoxiang.pojo.User is user; If there is an annotation, the alias is its annotation value.

    <package name="top.xcyxiaoxiang.pojo"/>
import org.apache.ibatis.type.Alias;

public class User {......}
// The alias is user_test

Summary: there are three ways to set aliases

  • Direct designation
  • Specify by package
  • Use annotation to specify

6. settings

These are extremely important tuning settings in MyBatis, which change the runtime behavior of MyBatis. Common and important configurations are as follows:

Set namedescribeEffective valueDefault value
cacheEnabledTurn all caches on or off globallytrue | falsetrue
lazyLoadingEnabledGlobal switch for delayed loading. When on, all associated objects are loaded late. In a specific association, the switch state of the item can be overridden by setting the fetchType property.true | falsefalse
logImplSpecify the specific implementation of the log used by MyBatis. If it is not specified, it will be found automatically.SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGINGNot set

7. mappers

Tell MyBatis where to find the mapper file, that is, register the mapper file.

  • Method 1 (resource path):
<!-- Use resource references relative to Classpaths -->
  <mapper resource="top/xcyxiaoxiang/Dao/UserMapper.xml"/>
  • Method 2 (fully qualified name):
<!-- Use the mapper interface to implement the fully qualified class name of the class -->
  <mapper class="org.mybatis.builder.AuthorMapper"/>

  • Method 3 (package name):
<!-- Register all the mapper interface implementations in the package as mappers -->
  <package name="org.mybatis.builder"/>

Note: f when using mode 2 and mode 3

  • The interface name must be the same as the mapper name.

  • mapper files and interfaces are in the same package.

Posted by vyb3 on Sat, 25 Sep 2021 11:44:36 -0700