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

Properties

The configuration file can be introduced through the properties property

Ideas:

properties can be configured externally and dynamically replaced

main idea:

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

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>

db.properties

driver=com.mysql.cj.jdbc.Driverurl=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=UTF8&useSSL=falseusername=rootpassword=123456
  • 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

mybatis-config.xml

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

UserMapp.xml

<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

mybatis-config.xml

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

UserMapp.xml

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