Mybatis configuration details

Time:2021-3-4

Mybatis configuration details

mybatis- config.xml Core profile

mybatis- config.xml The contents are as follows

  • Configuration
  • Properties
  • Settings
  • Typealiases (type aliases)
  • Typehandlers (type processors)
  • Objectfactory (object factory)
  • Plugins (plug-ins)
  • Environments (environment configuration)
  • Environment (environment variable)
  • Transaction manager
  • Datasource (data source)
  • Databaseidprovider (database vendor identification)
  • Mappers (mapper)

Pay attention to the order of element nodes! Wrong sequence will be reported

1. Environment element

<environments default="development">
 <environment id="development">
   <transactionManager type="JDBC">
     <property name="..." value="..."/>
   </transactionManager>
   <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>
 <environment id="test">
   <transactionManager type="JDBC">
     <property name="..." value="..."/>
   </transactionManager>
   <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>
  • To map SQL to different databases, you must specify a default environment, that is, default = development

1.1 sub element environment

Datasource data source (three built-in data source types)

        type="[UNPOOLED|POOLED|JNDI]")
  • Unpoled: the implementation of this data source just 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: the implementation of this data source is to be used in containers such as spring or application server. The container can centrally or externally configure the data source, and then place a JNDI context reference.

Note: there are many third-party implementations of data sources, such as DBCP, c3p0, Druid, etc

1.2 transaction manager transaction manager

        <transactionManager type="[ JDBC | MANAGED ]"/>

2. Mappers element (define mapping SQL statement file)

Where are the main files used to find SQL statements? You can use different ways to reference SQL statements. The specific reference methods are as follows

  • Using relative path to introduce SQL statement file
<! -- use resource references relative to Classpaths -- >
<mappers>
 <mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>
  • Use fully qualified resource locator (URL)
<! -- use fully qualified resource locator (URL) --- >
<mappers>
 <mapper url="file:///var/mappers/AuthorMapper.xml"/>
</mappers>
  • To use mapper interface to realize the fully qualified class name, the configuration file name and interface name should be consistent and in the same directory
<!--
Using mapper interface to realize fully qualified class name requires that the configuration file name and interface name are consistent and in the same directory
-->
<mappers>
 <mapper class="org.mybatis.builder.AuthorMapper"/>
</mappers>
  • All mapper interface implementations in the package should be registered as mappers, but the configuration file name should be consistent with the interface name and in the same directory
<!--
Register all mapper interface implementations in the package as mappers. However, the configuration file name and interface name should be consistent and in the same directory
-->
<mappers>
 <package name="org.mybatis.builder"/>
</mappers>

**Mapper configuration file
It mainly uses the methods in Dao interface and writes SQL statements, which is equivalent to the implementation of each method in the interface

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.xiezhr.dao.UserMapper">
    <select id="getUserList" resultType="com.xiezhr.pojo.User">
     select * from mybatis.user;
    </select>

    <insert id="addUser" parameterType="com.xiezhr.pojo.User">
        insert into mybatis.user values(#{id},#{name},#{pwd})
    </insert>

    <update id="updateUserById" parameterType="int">
        update  mybatis.user  Set name ='little dad 'where id = # {ID}
    </update>

    <delete id="deleteUserById" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete>
</mapper>

3.properties

  • We all know that in java development, some parameters are configured through the properties file. That’s what we’re going through db.properties File to configure the properties of the connection database

The specific steps are as follows

(1) Writing db.properties file

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8
username=root
password=123456

(2) Add an external configuration file to the mybatis core configuration file to connect to the database

<?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>
    <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="com/xiezhr/Dao/UserMapper.xml"></mapper>
    </mappers>
</configuration>

4. Typealiases

  • A type alias is a short name for a Java type. The significance is to reduce the redundancy of too long class names

(1) Custom JavaBean alias

<typeAliases>
        <typeAlias type="com.xiezhr.pojo.User" alias="user"/>
</typeAliases>

After the above configuration, you can use user anywhere com.xiezhr.pojo . user configuration alias can also be configured in the following way

(2) Configure all com.xiezhr.pojo The JavaBean alias under the package is a lowercase class name

<typeAliases>
   <package name="com.xiezhr.pojo"/>
</typeAliases>

After the above configuration, the following XML is equivalent

<select id="getUserList" resultType="user">
     select * from mybatis.user;
</select>
<select id="getUserList" resultType="com.xiezhr.pojo.user">
     select * from mybatis.user;
</select>

Other configurations [settings]

There are several common settings

  • Lazy loading
  • Log implementation
  • The opening and closing of cache

Here is a complete example of the setting element

<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>