Mybatis主文件配置

2020-07-23  本文已影响0人  Responsibility_
<!-- 加载外部JDBC配置文件 -->
    <properties resource="jdbc.properties">
    </properties>
     <settings>
    <!-- 开启日志 -->
    <setting name= "logImpl" value= "STDOUT_LOGGING"/>
    </settings>
<typeAliases>
    <typeAlias type="com.apesource.entity.Employee" alias="Employee"/>
    <typeAlias type="com.apesource.entity.EmployeeParam" alias="EmployeeParam"/>
    <typeAlias type="com.apesource.entity.Order" alias="Order"/>
    </typeAliases>
配置文件中读取数据信息

注意:读取配置文件value中用${}

property name="url" value="${jdbc_url}" />
<property name="username" value="${db_username}" />
<property name="password" value="${db_password}" />

示例

<environments default="development">
        <!-- 开发环境 -->
        <environment id="development">
            <!-- 事务管理器 -->
            <transactionManager type="JDBC" />
            <!-- 数据库连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="${driver_class}" />
                <!-- &符号不能直接使用转义为:$amp; -->
                <property name="url" value="${jdbc_url}" />
                <property name="username" value="${db_username}" />
                <property name="password" value="${db_password}" />
            </dataSource>
        </environment>
    </environments>
<!-- 映射管理 -->
    <mappers>
    <mapper resource="com/apesource/dao/mapper/EmployeeMapper.xml"/>
    <mapper resource="com/apesource/dao/mapper/OrderMapper.xml"/>
    </mappers>

配置文件示例

<?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>
    <!-- 加载外部JDBC配置文件 -->
    <properties resource="jdbc.properties">
    </properties>
    <!-- MyBatis配置 -->
    <settings>
    <!-- 开启日志 -->
    <setting name= "logImpl" value= "STDOUT_LOGGING"/>
    </settings>
    
    <!-- 实体类设置别名,在返回并保存数据库中的数据时用到-->
    <typeAliases>
    <typeAlias type="com.apesource.entity.Employee" alias="Employee"/>
    <typeAlias type="com.apesource.entity.EmployeeParam" alias="EmployeeParam"/>
    <typeAlias type="com.apesource.entity.Order" alias="Order"/>
    </typeAliases>
    
    <!-- 环境集 默认为为:development -->
    <environments default="development">
        <!-- 开发环境 -->
        <environment id="development">
            <!-- 事务管理器 -->
            <transactionManager type="JDBC" />
            <!-- 数据库连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="${driver_class}" />
                <!-- &符号不能直接使用转义为:$amp; -->
                <property name="url" value="${jdbc_url}" />
                <property name="username" value="${db_username}" />
                <property name="password" value="${db_password}" />
            </dataSource>
        </environment>
    </environments>
    <!-- 映射管理 -->
    <mappers>
    <mapper resource="com/apesource/dao/mapper/EmployeeMapper.xml"/>
    <mapper resource="com/apesource/dao/mapper/OrderMapper.xml"/>
    </mappers>

</configuration>

上一篇 下一篇

猜你喜欢

热点阅读