06_Configuration optimization

Property optimization

Environment configuration (environments)

Mybatis can configure multiple environments, but each SqlSessionFactory instance can only choose one environment

Mybatis default transaction manager is JDBC, connection pool: POOLED


The configuration file can be introduced through the properties property


properties can be configured externally and dynamically replaced

main idea:

Write a db.properties outside, and then import it in mybatis-config.xml


<?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/geng/dao/UserMapp.xml"/>    </mappers></configuration>


  • Can directly import external files
  • You can add some attribute configuration
  • If two files have the same field, the external configuration file is preferred!

Alias ​​optimization (Alias)

Type alias

Purpose: To reduce the length of the fully qualified name


<typeAliases>        <typeAlias type="com.geng.pojo.User" alias="uuuu"/></typeAliases>


<mapper namespace="com.geng.dao.UserDao">    <select id="getUserList" resultType="uuuu">        select * from mybatis.user    </select></mapper>

Package alias

Specify a package name, and mybatis will search for the required JavaBean under the package name. In the absence of annotations, the unqualified class name with the first letter and lowercase of Bean will be used as its alias


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


<mapper namespace="com.geng.dao.UserDao">    <select id="getUserList" resultType="user">        select * from mybatis.user    </select></mapper>

Set up

cacheEnabled , globally enable or disable any cache configured in all mapper configuration files;

lazyLoadingEnabled , a global switch for lazy loading. When turned on, all associated objects will be lazily loaded. The switch state of the item can be overridden by setting the fetchType property in a specific relationship.

logImpl , specify the specific implementation of the log used by Mybatis , it will be automatically searched if it is not specified.