天天看点

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>

    引用的时候类名的首字母大小写都可以

继续阅读