溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Properties在MyBatis?中的作用是什么

發(fā)布時間:2020-12-01 16:45:22 來源:億速云 閱讀:507 作者:Leah 欄目:編程語言

這篇文章將為大家詳細講解有關(guān)Properties在MyBatis 中的作用是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

1、我們將 數(shù)據(jù)庫的配置語句寫在 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>
<!-- 加載數(shù)據(jù)庫屬性文件 -->
<properties resource="db.properties">
</properties>
 <environments default="development">
 <environment id="development">
 <transactionManager type="JDBC"/>
 <!--dataSource 元素使用標準的 JDBC 數(shù)據(jù)源接口來配置 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>

  如果數(shù)據(jù)庫有變化,我們就可以通過修改 db.properties 文件來修改,而不用去修改 mybatis-configuration.xml 文件

注意:我們也可以在<properties></properties>中手動增加屬性

<!-- 加載數(shù)據(jù)庫屬性文件 -->
<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.");
 }
 /**
 * 根據(jù) properties 元素中的 resource 屬性讀取類路徑下屬性文件,并覆蓋properties 屬性中指定的同名屬性。
 */
 if (resource != null) {
 defaults.putAll(Resources.getResourceAsProperties(resource));
 } else if (url != null) {
 /**
 * 根據(jù)properties元素中的url屬性指定的路徑讀取屬性文件,并覆蓋properties 屬性中指定的同名屬性。
 */
 defaults.putAll(Resources.getUrlAsProperties(url));
 }
 /**
 * 獲取方法參數(shù)傳遞的properties
 * 創(chuàng)建XMLConfigBuilder實例時,this.configuration.setVariables(props);
 */
 Properties vars = configuration.getVariables();
 if (vars != null) {
 defaults.putAll(vars);
 }
 parser.setVariables(defaults);
 configuration.setVariables(defaults);
 }
}

通過源碼我們可以分析讀取優(yōu)先級:

    1、在 properties 內(nèi)部自定義的屬性值第一個被讀取

    2、然后讀取 resource 路徑表示文件中的屬性,如果有它會覆蓋已經(jīng)讀取的屬性;如果 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>

    這個時候你會發(fā)現(xiàn)無論你后臺傳給這個查詢語句什么參數(shù),都是 select * from user where username like '%root%'

mybatis 的別名配置  

  在 userMapper.xml 文件中,我們可以看到resultType 和 parameterType 需要指定,這這個值往往都是全路徑,不方便開發(fā),那么我們就可以對這些屬性進行一些別名設(shè)置

Properties在MyBatis?中的作用是什么

1、mybatis 默認支持的別名


  Properties在MyBatis?中的作用是什么

2、自定義別名  

  一、定義單個別名

    首先在全局配置文件 mybatis-configuration.xml 文件中添加如下配置:是在<configuration>標簽下

<!-- 定義別名 -->
<typeAliases>
 <typeAlias type="com.ys.po.User" alias="user"/>
</typeAliases>

    第二步通過 user 引用

Properties在MyBatis?中的作用是什么

  二、批量定義別名

    在全局配置文件 mybatis-configuration.xml 文件中添加如下配置:是在<configuration>標簽下

<!-- 定義別名 -->
<typeAliases>
 <!-- mybatis自動掃描包中的po類,自動定義別名,別名是類名(首字母大寫或小寫都可以,一般用小寫) -->
 <package name="com.ys.po"/>
</typeAliases>

關(guān)于Properties在MyBatis 中的作用是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI