myBatis的全局配置文件详解

myBatis的全局配置文件

mybatis封装需要的三要素
数据源
执行语句
操作者
SqlSessionFactoryBuilder

第一步 解析xml文件
configuration(配置)

001运行环境environment
dataSource数据源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT"/>
                <property name="username" value="root"/>
                <property name="password" value="wang0516"/>
            </dataSource>
        </environment>
        <environment id="product">
            <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/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT"/>
                <property name="username" value="root"/>
                <property name="password" value="wang0516"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

myBatis 可以配置成适应多个环境,现实情况下有多种理由需要这么做。例如,开发、测试和生产环境需要有不同的配置;
具体调用如下

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"product");

事务管理器(transactionManager)
<transactionManager type="JDBC"/>

002映射器(mappers)
既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要来定义 SQL 映射语句了。 但首先,我们需要告诉 MyBatis 到哪里去找到这些语句。

在自动查找资源方面,Java 并没有提供一个很好的解决方案,所以最好的办法是直接告诉 MyBatis 到哪里去找映射文件。

<! -- 001使用相对于类路径的资源引用 -->

<mappers>
<mapper resource="com/po/pf/repository/StudentMapper.xml"/>
</mappers>

<!-- 002使用映射器接口实现类的完全限定类名 -->

<mappers>
<mapper class="com.po.pf.repository.StudentMapper"/>
</mappers>

<!-- 003将包内的映射器接口实现全部注册为映射器 -->

<mappers>
<package name="com.po.pf.repository"/>
</mappers>

<!-- 004 使用完全限定资源定位符(URL) -->

<mappers>
<mapper url="file:D://StudentMapper.xml"/>
</mappers>

注意 其中class只能用于注解开发 resources和url只能用于xml配置文件开发
Package既能用于注解开发也能用于xml开发
还有个细节问题 假如你用了class也就是注解开发 但是同时你在对应的目录下也配置了xml

那mybatis启动访问就会报错 这是一个mybatis的bug

03 properties标签
这些属性可以在外部进行配置,并可以进行动态替换。你既可以在典型的 Java 属性文件中配置这些属性,也可以在 properties 元素的子元素中设置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<properties resource="jdbc.properties">
<property name="username" value="root"/>
<property name="jdbc_password" value="11111111"/>
</properties>
 
<dataSource type="POOLED">
<property name="driver" value="${jdbc_driverClassName}"/>
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_username}"/>
<property name="password" value="${jdbc_password}"/>
</dataSource>
 
jdbc_driverClassName=com.mysql.cj.jdbc.Driver
jdbc_url =jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
jdbc_username=root

如果一个属性在不止一个地方进行了配置,那么,MyBatis 将按照下面的顺序来加载:
首先读取在 properties 元素体内指定的属性。
然后根据 properties 元素中的 resource 属性读取类路径下属性文件,或根据 url 属性指定的路径读取属性文件,并覆盖之前读取过的同名属性。
最后读取作为方法参数传递的属性,并覆盖之前读取过的同名属性。
因此,通过方法参数传递的属性具有最高优先级,resource/url 属性中指定的配置文件次之,最低优先级的则是 properties 元素中指定的属性。

1
2
3
Properties po=new Properties();
po.setProperty("jdbc_password","11111111");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,"development",po);

004设置(settings)
这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。 下表描述了设置中各项设置的含义、默认值等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<settings>
    <setting name="cacheEnabled" value="true"/>
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="multipleResultSetsEnabled" value="true"/>
    <setting name="useColumnLabel" value="true"/>
    <setting name="useGeneratedKeys" value="false"/>
    <setting name="autoMappingBehavior" value="PARTIAL"/>
    <setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
    <setting name="defaultExecutorType" value="SIMPLE"/>
    <setting name="defaultStatementTimeout" value="25"/>
    <setting name="defaultFetchSize" value="100"/>
    <setting name="safeRowBoundsEnabled" value="false"/>
    <setting name="mapUnderscoreToCamelCase" value="false"/>
    <setting name="localCacheScope" value="SESSION"/>
    <setting name="jdbcTypeForNull" value="OTHER"/>
    <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
</settings>

cacheEnabled    全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。

005类型别名(typeAliases)和package
类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。


<typeAliases>
<typeAlias alias="Student" type="com.po.pf.domain.Student"/>
</typeAliases>

<select id="selectStudent" resultType="Student">
select * from student where id = #{id}
</select>

或者这样
<typeAliases>
<package name="com.po.pf.domain"/>
</typeAliases>

<select id="selectStudent" resultType="student">
select * from student where id = #{id}
</select>
每一个在包 com.po.pf.domain 中的 Java Bean,在没有注解的情况下,会使用 Bean 的首字母小写或大写都可以的非限定类名来作为它的别名。 不区分大小写

也可以使用注解
@Alias("su")
public class Student {
private Integer id;
private String name;

<select id="selectStudent" resultType="su">
select * from student where id = #{id}
</select>

 

posted @   __破  阅读(112)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示