properties以及别名定义
前面的示例中,在 mybatis-configuration.xml 的配置文件中,对数据库的配置都是硬编码在这个xml文件中,如下图,那么我们如何改进这个写法呢?
将数据库的配置语句写在 db.properties 文件中
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatisdemo
jdbc.username=root
jdbc.password=root
在 mybatis-configuration.xml中加载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 resource="db.properties">
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!--dataSource 元素使用标准的 JDBC 数据源接口来配置 JDBC 连接对象源 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
</configuration>
如果数据库有变化,我们就可以通过修改 db.properties 文件来修改,而不用去修改 mybatis-configuration.xml 文件。
注意:我们也可以在<properties></properties>中手动增加属性
<!-- 加载数据库属性文件 -->
<properties resource="db.properties">
<property name="username" value="aaa"/>
</properties>
那么这个时候读取的username 是以 db.properties 文件中的 root 为准,还是以自己配置的 aaa 为准呢?
我们先看一段 properties 文件加载的源码:
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
/**
* 解析properties 属性中指定的属性。
*/
Properties defaults = context.getChildrenAsProperties();
String resource = context.getStringAttribute("resource"); //resource 制定的属性路径
String url = context.getStringAttribute("url"); //url制定的属性路径
if (resource != null && url != null) {
throw new BuilderException("The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other.");
}
/**
* 根据 properties 元素中的 resource 属性读取类路径下属性文件,并覆盖properties 属性中指定的同名属性。
*/
if (resource != null) {
defaults.putAll(Resources.getResourceAsProperties(resource));
} else if (url != null) {
/**
* 根据properties元素中的url属性指定的路径读取属性文件,并覆盖properties 属性中指定的同名属性。
*/
defaults.putAll(Resources.getUrlAsProperties(url));
}
/**
* 获取方法参数传递的properties
* 创建XMLConfigBuilder实例时,this.configuration.setVariables(props);
*/
Properties vars = configuration.getVariables();
if (vars != null) {
defaults.putAll(vars);
}
parser.setVariables(defaults);
configuration.setVariables(defaults);
}
}
通过源码我们可以分析读取优先级:
- 在 properties 内部自定义的属性值第一个被读取
- 然后读取 resource 路径表示文件中的属性,如果有,它会覆盖已经读取的属性;如果 resource 路径不存在,那么读取 url 表示路径文件中的属性,如果有,它会覆盖第一步读取的属性值
- 最后读取 parameterType 传递的属性值,它会覆盖已读取的同名的属性
前面两步好理解,第三步我们可以举个例子来看:
我们在 userMapper.xml 文件中进行模糊查询
<select id="selectLikeUserName" resultType="com.harvey.java01.entity.User" parameterType="String">
select * from user where username like '%${jdbc.username}%'
<!-- select * from user where username like #{username} -->
</select>
这个时候你会发现无论你后台传给这个查询语句什么参数,都是 select * from user where username like '%root%'。
mybatis 的别名配置
在 userMapper.xml 文件中,我们可以看到resultType 和 parameterType 需要指定,这个值往往都是全路径,不方便开发,那么我们就可以对这些属性进行一些别名设置。
mybatis 默认支持的别名
自定义别名
① 定义单个别名
首先在全局配置文件 mybatis-configuration.xml 文件中添加如下配置:是在<configuration>标签下
<!-- 定义别名 -->
<typeAliases>
<typeAlias type="com.harvey.java01.entity.User" alias="user"/>
</typeAliases>
第二步通过 user 引用
② 批量定义别名
在全局配置文件 mybatis-configuration.xml 文件中添加如下配置:是在<configuration>标签下
<!-- 定义别名 -->
<typeAliases>
<!-- mybatis自动扫描包中的po类,自动定义别名,别名是类名(首字母大写或小写都可以,一般用小写) -->
<package name="com.harvey.java01.entity"/>
</typeAliases>
引用的时候类名的首字母大小写都可以
吃水不忘挖井人: |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!