The MyBatis configuration contains settings and properties.

properties

Properties can be configured in a typical Java Properties file instance, or passed in through sub-elements of the properties element.

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

The properties can then be used throughout the configuration files to substitute values that need to be dynamically configured.

<dataSource type="POOLED">
  <property name="driver" value="${driver}"/>
  <property name="url" value="${url}"/>
  <property name="username" value="${username}"/>
  <property name="password" value="${password}"/>
</dataSource>

settings

These are extremely important tweaks that modify the way that MyBatis behaves at runtime.

<settings>
  <setting name="cacheEnabled" value="true"/>
  <setting name="lazyLoadingEnabled" value="false"/>
  <setting name="aggressiveLazyLoading" value="false"/>
  <setting name="multipleResultSetsEnabled" value="true"/>
  <setting name="useColumnLabel" value="true"/>
</settings>

typeAliases

A type alias is simply a shorter name for a Java type.

typeHandlers

TypeHandler is used to retrieve the value in a means appropriate to the Java type.

Handling Enums

If you want to map an Enum, you'll need to use either EnumTypeHandler or EnumOrdinalTypeHandler.

objectFactory

Each time MyBatis creates a new instance of a result object, it uses an ObjectFactory instance to do so.

plugins

MyBatis allows you to intercept calls to at certain points within the execution of a mapped statement.

environments

MyBatis can be configured with multiple environments. This helps you to apply your SQL Maps to multiple databases for any number of reasons. if you want to connect to two databases, you need to create two instances of SqlSessionFactory.

databaseIdProvider

MyBatis is able to execute different statements depending on your database vendor.

mappers

Java doesn’t really provide any good means of auto-discovery in this regard, so the best way to do it is to simply tell MyBatis where to find the mapping files.

相关文章: