天天看點

mybatis 詳解(四)------properties以及别名定義

  上一篇部落格我們介紹了mybatis的增删改查入門執行個體,我們發現在 mybatis-configuration.xml 的配置檔案中,對資料庫的配置都是寫死在這個xml檔案中,如下圖,那麼我們如何改進這個寫法呢?

  

mybatis 詳解(四)------properties以及别名定義

1、我們将 資料庫的配置語句寫在 db.properties 檔案中

1

2

3

4

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檔案并讀取

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?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 檔案加載的源碼

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

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 需要指定,這這個值往往都是全路徑,不友善開發,那麼我們就可以對這些屬性進行一些别名設定

mybatis 詳解(四)------properties以及别名定義

1、mybatis 預設支援的别名

mybatis 詳解(四)------properties以及别名定義
mybatis 詳解(四)------properties以及别名定義

2、自定義别名  

  一、定義單個别名

    首先在全局配置檔案 mybatis-configuration.xml 檔案中添加如下配置:是在<configuration>标簽下

<!-- 定義别名 -->

<typeAliases>

<typeAlias type=

"com.ys.po.User"

alias=

"user"

/>

</typeAliases>

    第二步通過 user 引用

mybatis 詳解(四)------properties以及别名定義

  二、批量定義别名

    在全局配置檔案 mybatis-configuration.xml 檔案中添加如下配置:是在<configuration>标簽下

<!-- 定義别名 -->

<typeAliases>

<!-- mybatis自動掃描包中的po類,自動定義别名,别名是類名(首字母大寫或小寫都可以,一般用小寫) -->

<

package

name=

"com.ys.po"

/>

</typeAliases>

    引用的時候類名的首字母大小寫都可以

繼續閱讀