数据库
首页 > 数据库> > MyBatis中SqlMapConfig全局配置文件

MyBatis中SqlMapConfig全局配置文件

作者:互联网

在 WEB 工程中,对于 MyBatis 最核心的全局配置文件是 SqlMapConfig.xml 文件,其中包含了数据库的连接配置信息、Mapper 映射文件的加载路径、全局参数、类型别名等。

配置项说明

代码示例

<?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> 
    <!-- 1.properties属性引入外部配置文件 -->
    <properties resource="org/mybatis/example/config.properties">
        <!-- property里面的属性全局均可使用 -->
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </properties>
    <!-- 2.全局配置参数 -->
    <settings>
       <!-- 设置是否启用缓存 -->
       <setting name="cacheEnabled" value="true"/>
       <!-- 设置是否启用懒加载 -->
        <setting name="lazyLoadingEnabled" value="true"/>   
    </settings>
    <!-- 3.别名设置 -->
    <typeAliases>
        <typeAlias alias="student" type="cn.com.mybatis.student"/>
        <typeAlias alias="teacher" type="cn.com.mybatis.teacher"/>
        <typeAlias alias="integer" type="java.lang.Integer"/>
    </typeAliases>
    <!-- 4.类型转换器 -->
    <typeHandlers>
        <!-- 一个简单类型转换器 -->
        <typeHandler handler="org.mybatis.example.ExampleTypeHandler"/>
    </typeHandlers>
    <!-- 5.对象工厂 -->
    <objectFactory type="org.mybatis.example.ExampleObjectFactory">
        <!-- 对象工厂注入的参数 -->
        <property name="someProperty" value="100"/>
    </objectFactory>
    <!-- 6.插件 -->
    <plugins>
        <plugin interceptor="org.mybatis.example.ExamplePlugin">
            <property name="someProperty" value="100"/>
        </plugin>
    </plugins>
    <!-- 7.environments数据库环境配置 -->
    <!-- 和Spring整合后environments配置将被废除 -->
    <environments default="development">  
        <environment id="development"> 
            <!-- 使用JDBC事务管理 -->
            <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>
    <!-- 加载映射文件 -->
    <mappers>  
        <mapper resource="sqlmap/UserMapper.xml"/>
        <mapper resource="sqlmap/OtherMapper.xml"/>
    </mappers>  
</configuration>

 

标签:配置文件,标签,数据库,配置,SqlMapConfig,sql,MyBatis
来源: https://www.cnblogs.com/xfeiyun/p/15773260.html