MyBaties common XML file tag configuration

The MyBatis framework includes two types of XML files, one is a configuration file, namely mybatis-config.xml, and the other is a mapping file, such as Mapper.xml. MyBatis configuration files contain information about settings and properties that deeply affect the behavior of MyBatis. The hierarchy of configuration files is as follows:

  1. properties
  2. settings
  3. typeAliases (type aliases)

(1).properties tag

properties resource="org/mybatis/example/config.properties">
  <property name="username" value="root"/>
  <property name="password" value="root"/>
</properties>

If properties are configured in more than one place, MyBatis will load them in the following order:

(1) The properties specified in the properties element body are read first;
(2) Then read the property files under the class path according to the resource attribute in the properties element or read the property files according to the path specified by the url attribute, and overwrite the read property of the same name.
(3) Finally read the attribute passed as a method parameter and overwrite the already read attribute of the same name.

Therefore, properties passed via method parameters have the highest precedence, configuration files specified in the resource/url property come second, and properties specified in the properties property have the lowest precedence.

(2).settings tag

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

(3).typeAliases tag

A type alias is a short name set for a Java type. It is only related to XML configuration, and its significance is only to reduce the redundancy of fully qualified names of classes.

<typeAliases>
<typeAlias alias="user" type="cn.mybatis.domain.User"/>
</typeAliases>

When configured like this, user can be used anywhere cn.mybatis.domain.User is used.
You can also specify a package name, and MyBatis will search for the required JavaBean under the package name

<typeAliases>
<package name="cn.mybatis.domain"/>
</typeAliases>

Guess you like

Origin blog.csdn.net/qq_45349018/article/details/104869544