MyBatis~ configuration analysis, properties, settings, typeAliases, environments, mappers

  • The configuration file of MyBatis contains settings and attribute information that will deeply affect the behavior of MyBatis.
  • In the configuration file, you must ensure the order of the configuration information, otherwise an error will be reported, as shown below
    Insert picture description here

Properties

  • In the MyBatis configuration file, you can use the properties tag to import configuration information externally, or you can set the configuration information through the property tag in the tag
  1. Create a new config.properties file to write the configuration information, and then use the properties tag to get the configuration information
    Insert picture description here
    Insert picture description here
  2. Use the propetry tag to set configuration information
    <properties >
        <property name="username" value="root"/>
    </properties>
  • The set properties can be used in the entire configuration file to replace the property values ​​that need to be dynamically configured.
<environment id="development3">
            <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>
  • Note that the corresponding names must be the same, for example, username must correspond to username, and if external configuration files and properties are used at the same time, the priority is that the external configuration file has a higher priority

Settings

  • These are extremely important adjustment settings in MyBatis, and they will change the runtime behavior of MyBatis. The following table describes the meaning and default value of each setting in the settings.
<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>

Insert picture description here

Type aliases (typeAliases)

  • Setting a short name means reducing the fully qualified name and making the code cleaner. If we do not set an alias, then we must use the fully qualified name in the mapper file. If we set the alias, we can use our alias instead of the fully qualified name
    Insert picture description here
    Insert picture description here
  • Setting method
  1. DIY settings in the configuration file
<typeAliases>
  <typeAlias alias="Author" type="domain.blog.Author"/>
  <typeAlias alias="Blog" type="domain.blog.Blog"/>
  <typeAlias alias="Comment" type="domain.blog.Comment"/>
  <typeAlias alias="Post" type="domain.blog.Post"/>
  <typeAlias alias="Section" type="domain.blog.Section"/>
  <typeAlias alias="Tag" type="domain.blog.Tag"/>
</typeAliases>
  1. Given the automatic scanning settings of the package, this default generated alias is the full name with the first letter and lowercase, such as domain.blog.Author generated is author
<typeAliases>
  <package name="domain.blog"/>
</typeAliases>
  1. DIY alias by annotation
@Alias("author")
public class Author {
    
    
    ...
}

Insert picture description here

Environment configuration (environments)

  • MyBatis can be configured to adapt to multiple environments, this mechanism helps to apply SQL mapping to multiple databases
  • But remember: Although multiple environments can be configured, only one environment can be selected for each SqlSessionFactory instance.
  • If you want to connect to two databases, you need to create two SqlSessionFactory instances, one for each database. If there are three databases, three instances are required
  • environments Use default to bind which id is the time which environment
<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>
</environments>

Pay attention to some key points

The default environment ID (for example: default="development").
The environment ID defined by each environment element (for example: id="development").
The configuration of the transaction manager (for example: type="JDBC").
The configuration of the data source (for example: type="POOLED").

Transaction Manager (transactionManager)

  • There are two types of transaction managers
  • JDBC-This configuration directly uses the commit and rollback facilities of JDBC, which relies on the connection obtained from the data source to manage the transaction scope.
  • MANAGED-This configuration does almost nothing. It never commits or rolls back a connection, but lets the container manage the entire life cycle of the transaction (such as the context of a JEE application server). By default it will close the connection. So rarely use it
  • We often use JDBC by default

Data Source (dataSource)

  • 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
  • UNPOOLED-The implementation of this data source will open and close the connection every time it is requested. Although a bit slow, it is a good choice for simple applications that do not require high database connection availability.
  • POOLED-The realization of this data source uses the concept of "pool" to organize JDBC connection objects, avoiding the initialization and authentication time necessary to create a new connection instance. This approach is very popular and enables concurrent web applications to respond quickly to requests.
  • JNDI-This data source is implemented in order to be used in containers such as EJB or application servers. The container can configure the data source centrally or externally, and then place a JNDI context data source reference.
  • We often use POOLED by default

Mapper description (mappers)

  • Use mappers element to bind our mapper file
  • The most commonly used is to use resource references relative to the classpath. This is very free to use and bind the mapper directly
<mappers>
  <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
  <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
  <mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>
  • Use the fully qualified class name of the mapper interface to implement the class. Using this must ensure that the mapper.xml file and the interface entity class are in the same directory and must have the same name
<mappers>
  <mapper class="org.mybatis.builder.AuthorMapper"/>
  <mapper class="org.mybatis.builder.BlogMapper"/>
  <mapper class="org.mybatis.builder.PostMapper"/>
</mappers>

Insert picture description here

  • Register all the mapper files in the package to become a mapper. To use this requirement, the mapper must be the same as the above. The xml file and the interface entity class must be in the same directory and must have the same name
<mappers>
  <package name="org.mybatis.builder"/>
</mappers>

Guess you like

Origin blog.csdn.net/Shangxingya/article/details/109326479