Mybatis - XML configuration

<?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>
    <!-- global variable -->
    <properties>
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/db_mybatis"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </properties>

    <!--Configuration environment-->
    <environments default="development">
        <environment id="development">
            <!-- Configure transaction type -->
            <transactionManager type="JDBC"/>
            <!-- Configure data sources (connection pools) -->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    
	<typeAliases>
        <typeAlias type="com.minifull.pojo.User" alias="user"/>
    </typeAliases>

    <!-- Specify mapping file -->
    <mappers>
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>

1. properties

  • Using the resources property to import an external configuration file (common)
  • Adopt global internal configuration
  • Importing an external profile using the url property

Use the resources property to introduce an external configuration file (common)

JDBC config.properties, there is no limit to the configuration file name, but the configuration file must be placed in the classpath

# The key is jdbc..xxx Can be modified by yourself
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_mybatis
jdbc.username=root
jdbc.password=123456

Modify Mybatis configuration file

<!-- Import external files  -->
    <properties resource="jdbcConfig.properties"/>

    <!--Configuration environment-->
    <environments default="development">
        <environment id="development">
            <!-- Configure transaction type -->
            <transactionManager type="JDBC"/>
            <!-- Configure data sources (connection pools) -->
            <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>

Adopt global internal configuration

<properties>
	<property name="driver" value="com.mysql.jdbc.Driver"/>
	<property name="url" value="jdbc:mysql://localhost:3306/db_mybatis"/>
	<property name="username" value="root"/>
	<property name="password" value="123456"/>
</properties>

Importing an external profile using the url property

The external file of this method can be placed anywhere, but the path writing method must be in the way of Url. The file protocol used here

<!-- Import external files  -->
<properties url="file:///D:/document/IdeaProjects/java_web_ssm/my_mybatis/src/main/resources/jdbcConfig.properties"/>

  • URL: uniform resource locator. It can uniquely identify the location of a resource and consists of four parts: protocol, host, port and path

    • For example: http://localhost:8080/mybatisserver/demo1, where HTTP is the protocol, localhost is the host, 8080 is the port number, and / mybatisserver/demo1 is the uri (path)
  • URI: Uniform Resource Identifier. It can only locate a resource in the application

2. typeAliases label

When you assign an alias to an entity class, you can use the typeAliases label, which is case insensitive

<configuration>
    <!--Configure aliases-->
    <typeAliases>
        <typeAlias type="com.minifull.pojo.User" alias="user"/>
    </typeAliases>
    <!-- Other configurations omitted... -->
</configuration>

  • The typeAlias child label is used to configure aliases. The type attribute specifies the fully qualified class name of the class to be configured (this class can only be a domain entity class), and the alias attribute specifies the alias. Once an alias is specified, it is no longer case sensitive
  • In other words, at this time, we can write resultType = "user" in the mapping file, or write resultType = "user"

3. package

Can be used in two places
① When we have multiple entity classes that need to be aliased, we can use the package tag

The package label specifies the package to configure the alias. When specified, all entity classes under the package will register the alias, and the alias is the class name, which is no longer case sensitive

<typeAliases>
    <! -- aliasing of all entities under the package -- >
    <package name="com.minifull.pojo"/>
</typeAliases>

The package tag can also register all the mapper interface implementations in a package as mappers

After this configuration, we do not need to configure the Mapper interface one by one. However, the premise of this configuration is that the mapping profile location must be the same as the package structure of the dao interface

<!-- Specify mapping file -->
<mappers>
    <package name="com.minifull.mapper"/>
</mappers>
Published 146 original articles, won praise 3, visited 4878
Private letter follow

Tags: JDBC MySQL Mybatis xml

Posted on Sat, 14 Mar 2020 22:28:53 -0700 by johnseito