1、核心配置文件

MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。 配置文档的顶层结构如下:

环境配置(environments)

可以配置多套运行环境,但是每个SqlSessionFactory实例只能选择一种运行环境,要知道如何配置多套运行环境,Mybatis默认的事务管理器就是JDBC,连接池:POOLED

属性(properties)

可以通过properties属性来实现引用配置文件(db.properties)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSl=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username=root
password=root
<!--引入外部配置文件,注意这应该放在<configuration>设置最上层-->
<properties resource="db.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>

类型别名

类型别名是为java类型设置一个短的名字,它和XML配置有关

<select id="getUserList" resultType="top.ltyzqhh.pojo.User">
        select * from mybatis.user;
    </select>
<select id="getUserList" resultType="User">
        select * from mybatis.user;
    </select>

相当于简化了类名

需要设置typeAliases级别小于properties

<typeAliases>
        <typeAlias alias="User" type="top.ltyzqhh.pojo.User"/>
    </typeAliases>

也可以指定包名下的实体类,这样的别名就是具体实体类的名字(首字母是小写),如果有注解则为注解名

<typeAliases>
        <package name="top.ltyzqhh.pojo"/>
    </typeAliases>

有别名注解的情况

Untitled

Untitled