Mybaties- introduction of external configuration files and aliases

mybatis-config.xml

<?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 核心配置文件 -->
<configuration>

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

    <!--可以给实体类起别名-->
    <typeAliases>
        <typeAlias type="com.kuang.pojo.User" alias="User"/>
    </typeAliases>

    <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>
    <!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册!-->
    <mappers>
        <mapper resource="com/kuang/dao/UserMapper.xml"/>
        <!--  <mapper class="com.kuang.dao.UserMapper"/> -->
        <!-- <package name="com.kuang.dao"/> -->
    </mappers>
</configuration>

db.properties Note: External files priorities set higher than the core document 

driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&usUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=UTC
username = root
password = lgz0921

 

He published 195 original articles · won praise 27 · views 20000 +

Guess you like

Origin blog.csdn.net/lgz0921/article/details/104099137