mybatis核心配置文件

记录在mybatis核心配置文件中,常用的配置选项:

下边是之前的配置选项:

<?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>
    <environments default="development">
        <!--开发环境 - 数据库配置信息-->
        <environment id="development"><!--配置的唯一-->
            <transactionmanager type="jdbc"/>
            <datasource type="pooled">
                <property name="driver" value="com.mysql.cj.jdbc.driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/java_pro?servertimezone=asia/shanghai&amp;useunicode=true&amp;characterencoding=utf8&amp;usessl=false "/>
                <property name="username" value="root"/>
                <property name="password" value="lvxingchen@123"/>
            </datasource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/lxc/dao/usermapper.xml"></mapper>
    </mappers>
</configuration>

一、属性(properties)

在mybatis配置文件中,它提供了一个属性标签 <properties> , 这个属性标签跟 xxx.properties文件一样,配置一些基础信息,只不过现在又提供了一个标签形式。

我们把配置文件中基础信息单独写在db.properties文件中

<?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标签-->
    <properties resource="db.properties"></properties>
    <environments default="development">
        <!--开发环境 - 数据库配置信息-->
        <environment id="development"><!--配置的唯一-->
            <transactionmanager type="jdbc"/>
            <datasource type="pooled">
                <property name="driver" value="#{driver}"/><!--用#{}占位,里边是配置文件的key -->
                <property name="url" value="#{url}"/>
                <property name="username" value="#{username}"/>
                <property name="password" value="#{password}"/>
            </datasource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/lxc/dao/usermapper.xml"></mapper>
    </mappers>
</configuration>

还可以在属性标签里添加配置标签,
注意:如果同时配置 配置文件中 和 <property>,那么配置文件优先级高。

<configuration>
    <properties resource="db.properties">
        <property name="username" value="root" />
        <property name="password" value="lvxingchen" />
    </properties>
    <!-- ··· ··· -->
</configuration>

注意一个问题:
在配置文件中,url 路径中有 & 符号,会使用 &amp; 来代替,当我们把url抽离出来,放到xxx.properties 文件中时,需要把  &amp; 改成 & 。

二、设置(settings)

cacheenabled

lazyloadingenabled

logimpl

三、类名别名(typealiases)

下边记录类名别名的三种方式:

方式一:

类名别名,仅用于xml配置,意在降低冗余的全限定类名的书写,例如:

在mybatis核心配置文件中编写类名别名:

<?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 -》 typealiases-》 environments -->
    <properties resource="db.properties"></properties>
    <!--别名配置: com.lxc.domain.user的别名为user-->
    <typealiases>
        <typealias alias="user" type="com.lxc.domain.user" />
    </typealiases>
    <environments default="development">
    <!--  ···  -->
    </environments>
    <mappers></mappers>
</configuration>

 在sql映射文件中使用别名:

<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper
        public "-//mybatis.org//dtd mapper 3.0//en"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lxc.dao.usermapper">
    <!--现在的配置-->
    <select id="getuserlist" resulttype="user">
        select * from mybatis
    </select>
    <!--原来的配置
    <select id="getuserlist" resulttype="com.lxc.domain.user">
        select * from mybatis
    </select>-->
</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>
    <properties resource="db.properties"></properties>
    <!--包名的别名配置-->
    <typealiases>
        <package name="com.lxc.domain" />
    </typealiases>
    <environments default="development">
    <!--  ···  -->
    </environments>
    <mappers></mappers>
</configuration>

在使用的时候注意:

每一个在包 com.lxc.domain 中的 java bean,在没有注解的情况下,会使用 bean 的首字母小写的非限定类名来作为它的别名。 比如 com.lxc.domain下边的user类,的别名为 user;
mybatis 会在包名下边搜索需要的java bean(这个java bean 就是 下图domain里边的类文件)。
 com.lxc.domain.user 的别名为 user

方式三:

注解方式,这种方式其实是第二种方式的延伸,mybatis 会在包名下边搜索需要的java bean, 若有注解,则别名为注解的值,看下边代码:

使用这种方式前提必须在mybatis核心配置文件中配置:
    <typealiases>
        <package name=”com.lxc.domain” />
    </typealiases>

@alias("instanceusers")
public class user {
}

此时 com.lxc.domain.user 的别名为 instanceusers

四、映射器(mappers)

最常用的配置

<!--每一个mapper.xml(sql映射文件)都需要在mybatis核心文件中注册!-->
<mappers>
    <mapper resource="com/lxc/dao/usermapper.xml"></mapper>
</mappers>

到此这篇关于详解mybatis核心配置文件的文章就介绍到这了,更多相关mybatis核心配置文件内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!