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>
    <!-- 全局变量 -->
    <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>

    <!--配置环境-->
    <environments default="development">
        <environment id="development">
            <!-- 配置事务类型 -->
            <transactionManager type="JDBC"/>
            <!-- 配置数据源(连接池) -->
            <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>

    <!-- 指定映射文件 -->
    <mappers>
        <mapper resource="UserMapper.xml"/>
    </mappers>
</configuration>

1. properties property

  • Using the resources introduced into an external configuration file attributes (common)
  • Use the global internal configuration
  • Introducing the outside using the url attribute profile

Using the resources introduced into an external configuration file attributes (common)

jdbcConfig.properties, profile name is not limited, but the configuration file must be placed on the classpath

# 键为 jdbc.xxx 可以自行修改
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_mybatis
jdbc.username=root
jdbc.password=123456

Mybatis modify configuration files

<!-- 引入外部文件  -->
    <properties resource="jdbcConfig.properties"/>

    <!--配置环境-->
    <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}"/>
            </dataSource>
        </environment>
    </environments>

Use the 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>

Introducing the outside using the url attribute profile

The method of external file can be placed anywhere, but must follow the path of the wording Url manner, file protocols used herein

<!-- 引入外部文件  -->
<properties url="file:///D:/document/IdeaProjects/java_web_ssm/my_mybatis/src/main/resources/jdbcConfig.properties"/>

  • URL: Uniform Resouce LOcator, or Uniform Resource Locator. It uniquely identifies the location of a resource, consists of four parts: the protocol, host, port, path

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

2. typeAliasesLabel

To an entity like an alias when you can use typeAliasesthe label, the use of an alias is not case sensitive

<configuration>
    <!--配置别名-->
    <typeAliases>
        <typeAlias type="com.minifull.pojo.User" alias="user"/>
    </typeAliases>
    <!-- 其他配置省略... -->
</configuration>

  • typeAlias ​​subtag for alias. Wherein the type attribute is used to specify the configuration of the fully qualified class name of the class (the class is only a domain entity class), alias attribute specifies the alias. Once the alias is specified, the alias is no longer case sensitive
  • In other words, at this time we can write resultType in the mapping file = "user", can also write resultType = "USER"

3. package

Can be used in two places
① When we have multiple aliases entity classes, then we can use packagelabels

The tag specifies the alias configuration package to package, when specified, all entities registered class will alias in the packet, and the class name is an alias, not case-sensitive

<typeAliases>
    <!-- 包下所有实体类起别名 -->
    <package name="com.minifull.pojo"/>
</typeAliases>

package Tags can also map a packet interface within the realization of the entire registered as a mapper

After this configuration, we do not need one by one to configure the Mapper interfaces. However, the premise of this configuration is that the position of the mapping profile and have the same packet structure interfaces dao

<!-- 指定映射文件 -->
<mappers>
    <package name="com.minifull.mapper"/>
</mappers>
Published 147 original articles · won praise 3 · Views 4886

Guess you like

Origin blog.csdn.net/weixin_43907800/article/details/104876849