这篇文章将为大家详细讲解有关Properties在MyBatis 中的作用是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1、我们将 数据库的配置语句写在 db.properties 文件中
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=root
2、在 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);
}
}
通过源码我们可以分析读取优先级:
1、在 properties 内部自定义的属性值第一个被读取
2、然后读取 resource 路径表示文件中的属性,如果有它会覆盖已经读取的属性;如果 resource 路径不存在,那么读取 url 表示路径文件中的属性,如果有它会覆盖第一步读取的属性值
3、最后读取 parameterType 传递的属性值,它会覆盖已读取的同名的属性
前面两步好理解,第三步我们可以举个例子来看:
我们在 userMapper.xml 文件中进行模糊查询
<select id="selectLikeUserName" resultType="com.ys.po.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 需要指定,这这个值往往都是全路径,不方便开发,那么我们就可以对这些属性进行一些别名设置
1、mybatis 默认支持的别名
2、自定义别名
一、定义单个别名
首先在全局配置文件 mybatis-configuration.xml 文件中添加如下配置:是在<configuration>标签下
<!-- 定义别名 -->
<typeAliases>
<typeAlias type="com.ys.po.User" alias="user"/>
</typeAliases>
第二步通过 user 引用
二、批量定义别名
在全局配置文件 mybatis-configuration.xml
文件中添加如下配置:是在<configuration>
标签下
<!-- 定义别名 -->
<typeAliases>
<!-- mybatis自动扫描包中的po类,自动定义别名,别名是类名(首字母大写或小写都可以,一般用小写) -->
<package name="com.ys.po"/>
</typeAliases>
关于Properties在MyBatis 中的作用是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。