MyBaits Basic Core Configuration

1. mybaits-config core profile label

Integrating Mybaits into Spring requires adding some configuration files. Its core configuration file is mybaits-config.xml.

1.1, configuration Label

The configurationtag is the parent of all tags and corresponds to the configuration class configuration for mybaits.

1.2, Properrties tag

Properrties is a label used to import a property file to add a database connection configuration. This label may not exist, but the database connection configuration of mybaits-config.xml requires hard coding. A corresponding property file allows you to configure configuration information in a property file.

<properties resource=""></properties>

1.3, typeAliases tag

An alias tag that specifies an alias for a single object so that in the Mybaits framework's xml file, all locations where the full path name of the object needs to be written can be aliased.

In addition to user-defined alias tags, Mybaits also defines aliases that are mapped in the TypeAliasRegistry class

    <typeAlias alias="user" type=""/>

1.4, typeHandler tag

There are methods for converting database types to java data types in the TypeHandlerRegistry class. After downloading the source code, look again to see that all types of handlers inherit BaseTypeHandler for type conversion. If you want to do some special conversion, you need to write a custom handler, inherit BaseTypeHandler, override the conversion method, andAdd to the typeHandler tag.

    <typeHandler handler=""/>

Once the definition is complete, use the typeHandler after the required field to indicate the handler used.

<resultMap id="BaseResultMap" type="">
    <id column="user_id" jdbcType="BIGINT" property="userId" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" 
    <result column="user_age" jdbcType="INTEGER" property="userAge" />
    <result column="user_sex" jdbcType="INTEGER" property="userSex" />

1.5, objectFactory tag

There are four methods within the ObjectFactory interface, which Mybaits uses to create corresponding objects. It has a default implementation class, DefaultObjectFactory. If a developer wants to use some other actions when creating objects, he or she can customize a class by implementing the ObjectHandler or inheriting the DefaultObjectFactory and declare it in the ObjectHandler tag.

There are also property subtags under the objectHandler tag, which define some key-value pairs

<objectFactory type="">
    <property name="test" value="one"/>

1.6, plugins tag

Plug-in tags, which can be used to extend Mybaits functionality, can only block critical methods in the four plug-ins. When defining multiple plug-ins, the execution order of the plug-ins is bottom-up.

    <plugin interceptor="">
        <property name="test" value="one"/>
    <plugin interceptor=""/>

1.7, Environment Label

Environment Label Environment Configuration is used to configure the database environment. Each environment label represents a data source, transactionManager is responsible for configuring transactions, datasource label represents a data source, POOLED represents a connection pool, and UNPOOLED represents a database connection configuration without a connection pool property.

<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC"/>
        <dataSource type="POOLED">
            <property name="driver" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>

1.8, mappers tag

Specify the startup path file, you can specify

  1. Class Path Resource Reference
  2. Fully qualified resource locator
  3. Implement fully qualified class names for classes using mapper interfaces
  4. Register all mapper interface implementations within the package as mappers
    <mapper resource="BlogMapper.xml"/>
    <mapper resource="BlogMapperExt.xml"/>

1.9, settings tag


Determining whether to turn on secondary caching in a project


Level 1 Cache Scope


lazyLoadingEnabled = false, then delayed loading is disabled and all associated object data is cascaded
lazyLoadingEnabled = true, mybatis is loaded with hierarchical delay by default.


aggressiveLazyLoading = true, mybatis is loaded with hierarchical delay
aggressiveLazyLoading = false, mybatis loaded on demand.


Delayed Loading


Executor type, SIMPLE common executor, REUSE reusable executor, BATCH batch operation data


Log implementation

2. Mapper Mapping File Label


Cache configuration for a given namespace


References to other namespace cache configurations


Describes how to load objects from a database result set


Reusable SQL statements that can be referenced by other statements


Mapping Insert Statement


Mapping Update Statement


Map Delete Statement


Mapping Lookup Statement

Keywords: Java Mybaits

Added by pelleas1022 on Tue, 12 Oct 2021 19:20:46 +0300