Learn Mybatis from scratch - Mybatis core configuration

  • Properties

Define attributes and read attribute files

  1. Define properties file

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/development
jdbc.username=root
jdbc.password=123456

  1. Import the properties file into the sqlMappingConfig file
<?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>
    <!--use resource Import external property profile-->
    <properties resource="db.properties">
        <!--stay properties Internal use property Define properties-->
        <!--If the external configuration file has this attribute, the internally defined attribute will be overwritten by the external attribute-->
        <property name="jdbc.username" value="root"/>
        <property name="jdbc.password" value="1234"/>
    </properties>
    <environments default="development">
        <environment id="development">
            <!--use jdbc transaction management-->
            <transactionManager type="JDBC"/>
            <!--Configure database connection pool-->
            <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}"/>
            </dataSource>
        </environment>
    </environments>
    <!--Load mapping file-->
    <mappers>
        <!--<mapper resource="demo1/Mapping"/>-->
        <mapper resource="demo2/Mapping"/>
    </mappers>
</configuration>

After using properties configuration

  • Settings

This is a very important adjustment setting in Mybatis. They will change the runtime behavior of Mybatis

<!-- Used to configure settings in Mybatis -- >
<settings>
    <!-- Enable hump mapping to serve customized SQL statements -- >
    <!-- Set the hump naming attribute that enables the mapping of database field underscores to java objects. The defau lt is false -- >
    <setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>

The sqlMappingConfig file 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>
    <!--use resource Import external property profile-->
    <properties resource="db.properties">
        <!--stay properties Internal use property Define properties-->
        <!--If the external configuration file has this attribute, the internally defined attribute will be overwritten by the external attribute-->
        <property name="jdbc.username" value="root"/>
        <property name="jdbc.password" value="1234"/>
    </properties>
    <!--Used to configure Mybatis Settings in-->
    <settings>
        <!--Turn on hump mapping, which is user-defined SQL Statement service-->
        <!--Set enable database field underline mapping to java Hump naming attribute of the object. The default is false-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <environments default="development">
        <environment id="development">
            <!--use jdbc transaction management-->
            <transactionManager type="JDBC"/>
            <!--Configure database connection pool-->
            <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}"/>
            </dataSource>
        </environment>
    </environments>
    <!--Load mapping file-->
    <mappers>
        <!--<mapper resource="demo1/Mapping"/>-->
        <mapper resource="demo2/Mapping"/>
    </mappers>
</configuration>

  

  • typeAliases

The function of type alias is to set a short name for Java type for our subsequent writing

<typeAliases>
    <!-- Single alias definition -- >
    <typeAlias alias="Customer" type="demo2.Customer"/>
    <!-- Batch alias definition, scan the classes under the whole package, and the alias is the class name (case insensitive) - >
    <!-- Note: if the current package class and the sub package class have the same name, an exception will be reported. We can use the annotation @ Alias ("Alias") -- > on the class
    <package name="demo2"/>
</typeAliases>

After the alias is defined, the alias can be used in Mapping

  • typeHandlers

        Whether Mybatis sets a parameter in the PreparedStatement or takes a value from the result set, it will use the type processor to convert the obtained value into Java type in an appropriate way. Before Mybatis 3.4, we need to register these processors manually, but in later versions, the processors are registered automatically. Just need to know

  • Plugins

Plug in is a very powerful mechanism provided by Mybatis, which allows us to modify some core behaviors of Mybatis through plug-ins

  • Environments

Mybatis can configure a variety of environments, such as development environment, test environment and production environment. Each environment needs different configurations, so we can configure each environment with an environment tag and specify a unique identifier id. Quickly switch environments by specifying an environment identifier through the default attribute in the environments tab

Environment sublabel description

1. Transaction manager transaction management

The value of type (JDBC/MANAGED / user defined) has the following differences:

JDBC: use the commit and rollback settings of JDBC to manage the transaction scope depending on the connection from the data source

MANAGED: do not commit or roll back a connection, let the container manage the whole life cycle of the transaction, ManagedTransactionFactory

Custom: implement TransactionFactory interface. At this time, type = full class name / alias

2. dataSource datasource

type has the following values

UNPOOLED: do not use connection pool UnpooledDataSourceFactory

POOLED: PooledDataSourceFactory using connection pool

JNDI: find the specified data source in containers such as EJB or application server

Custom: implement the DataSourceFactory interface and define the data source acquisition method

During actual development, we will use Spring to manage data sources and configure transaction control to override the configuration of the above data sources

  • databaseIDProvider

Mybatis can execute different sql statements according to different database manufacturers. Therefore, we can set different vendors through the databaseIDProvider tag

<!-- Configuring databases from different vendors -- >
<databaseIdProvider type="DB_VENDOR">
    <property name="MYSQL" value="mysql"/>
    <property name="DB2" value="db2"/>
    <property name="Oracle" value="oracle"/>
    <property name="SQL Server" value="sqlserver"/>
</databaseIdProvider>

  • Mappers
  1. <mapper resource=" ">

Use resources relative to classpath

  1. <mapper class=" ">

When using the mapper interface class path, the mapper interface name and mapper mapping file name should be the same and placed in the same directory

  1. <package name=" ">

Specify all mapper interfaces under the package. The mapper interface name and mapper mapping file name shall be the same and placed in the same directory

Tags: Java Database MySQL Mybatis

Posted on Fri, 10 Sep 2021 18:09:18 -0400 by rocram16