Mybatis XML configuration details

  • 2020-05-27 05:28:22
  • OfStack

Mybatis is often configured with XML with caching disabled


<?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>
 <!-- Settings  You have to put it on top  -->
 <settings>
  <!--  This configuration affects the global switch for all caches configured in the mapper. The default true -->
  <setting name="cacheEnabled" value="false" />  
  <!-- MyBatis  Leverage the local caching mechanism ( Local Cache ) prevent circular references ( circular references ) and accelerating the repetition of nested queries.  
        The default value is  SESSION , in which case it will be cached 1 All queries executed in 1, 2 sessions. 
        If set to  STATEMENT , the local session is used only for statement execution, same for both  SqlSession  The data will not be Shared between the different calls. 
   -->
   <setting name="localCacheScope" value="SESSION" />
   <!--  When no specific parameter is provided  JDBC  Type is specified as a null value  JDBC  Type.   Some drivers need to specify the column  JDBC  Type, mostly used directly 1 General type can be, for example  NULL , VARCHAR  or  OTHER .  -->
  <setting name="jdbcTypeForNull" value="OTHER" />
 </settings>
 <!-- MyBatis  The connection MySql The database  -->
 <environments default="development">
  <environment id="development">
   <!--  use jdbc Transaction management  -->
   <transactionManager type="JDBC" />
   <!--  Configure the database connection pool  -->
   <dataSource type="POOLED">
    <property name="driver" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/yourdb" />
    <property name="username" value="root" />
    <property name="password" value="toor" />
   </dataSource>
  </environment>
 </environments>
 <!--  All database statement mapping files must be registered here  -->
 <mappers>
  <mapper resource="dao/mappers/ManagerMapper.xml" />
 </mappers>
</configuration>

The full MyBatis configuration is as follows:


<?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>
 <!-- Settings  You have to put it on top  -->
 <settings>
  <!--  This configuration affects the global switch for all caches configured in the mapper. The default true -->
  <setting name="cacheEnabled" value="true" />
  <!--  Global switch for lazy loading. When enabled, all associated objects are lazily loaded.   Can be set in a particular association relationship fetchType Property to override the switch state of the item. The default false -->
  <setting name="lazyLoadingEnabled" value="true" />
  <!--  Whether to allow the bill 1 Statement returns multiple result sets (drivers need to be compatible).   The default true -->
  <setting name="multipleResultSetsEnabled" value="true" />
  <!--  Use column labels instead of column names. Different drivers will behave differently in this respect,   You can refer to the driver documentation or test the two different patterns to see the results of the drivers used. The default true -->
  <setting name="useColumnLabel" value="true" />
  <!--  allow  JDBC  Automatic primary key generation is supported and driver compatibility is required.   If I set it to  true  This setting forces the use of automatic primary key generation, though 1 Some drivers are not compatible but still work (e.g  Derby ). 
     The default false -->
  <setting name="useGeneratedKeys" value="false" />
  <!--  The specified  MyBatis  How columns should be automatically mapped to fields or properties.  NONE  Means cancel automatic mapping; PARTIAL  Only the automatic mapping does not define the result set of the nested result set mapping. 
    FULL  Any complex result set (whether nested or not) is automatically mapped. 
     The default  PARTIAL -->
  <setting name="autoMappingBehavior" value="PARTIAL" />
  <!-- 
  Specify the behavior when detects an unknown column (or unknown property type) of automatic mapping target.
   NONE: Do nothing
   WARNING: Output warning log (The log level of 'org.apache.ibatis.session.AutoMappingUnknownColumnBehavior' must be set to WARN)
   FAILING: Fail mapping (Throw SqlSessionException)
  Default:NONE
   -->
  <setting name="autoMappingUnknownColumnBehavior" value="WARNING" />
  <!--  Configure the default actuator. SIMPLE  It's an ordinary actuator; REUSE  The executor reuses the preprocessed statement ( prepared statements );  BATCH  The executor reuses statements and performs batch updates. The default SIMPLE -->
  <setting name="defaultExecutorType" value="SIMPLE" />
  <!--  Sets the timeout, which determines the number of seconds the driver waits for the database response. Not Set (null) -->
  <setting name="defaultStatementTimeout" value="25" />
  <!--  Get the number of ( fetchSize ) setting 1 Two prompt values. This parameter can only be overridden in query Settings.  -->
  <setting name="defaultFetchSize" value="100" />
  <!--  Allows the use of paging in nested statements ( RowBounds ).  If allow, set the false. -->
  <setting name="safeRowBoundsEnabled" value="false" />
  <!--  Whether to turn on automatic hump naming rules ( camel case ) mapping, that is, from classic database column names  A_COLUMN  To the classic  Java  The property name  aColumn  Similar mapping.  -->
  <setting name="mapUnderscoreToCamelCase" value="false" />
  <!-- MyBatis  Leverage the local caching mechanism ( Local Cache ) prevent circular references ( circular references ) and accelerating the repetition of nested queries.  
        The default value is  SESSION , in which case it will be cached 1 All queries executed in 1, 2 sessions. 
        If set to  STATEMENT , the local session is used only for statement execution, same for both  SqlSession  The data will not be Shared between the different calls. 
   -->
   <setting name="localCacheScope" value="SESSION" />
   <!--  When no specific parameter is provided  JDBC  Type is specified as a null value  JDBC  Type.   Some drivers need to specify the column  JDBC  Type, mostly used directly 1 General type can be, for example  NULL , VARCHAR  or  OTHER .  -->
  <setting name="jdbcTypeForNull" value="OTHER" />
  <!--  Specifies which object's method fires 1 Secondary lazy loading.  -->
  <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString" />
 </settings>
 <!-- MyBatis  The connection MySql The database  -->
 <environments default="development">
  <environment id="development">
   <!--  use jdbc Transaction management  -->
   <transactionManager type="JDBC" />
   <!--  Configure the database connection pool  -->
   <dataSource type="POOLED">
    <property name="driver" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/yourdb" />
    <property name="username" value="root" />
    <property name="password" value="toor" />
   </dataSource>
  </environment>
 </environments>
 <!--  All database statement mapping files must be registered here  -->
 <mappers>
  <mapper resource="dao/mappers/ManagerMapper.xml" />
 </mappers>
</configuration>

Mybatis website document: http: / / www mybatis. org/mybatis - 3 / zh index. html


Related articles: