mybatis 详解(四)------properties以及别名定义

 上一篇博客咱们介绍了mybatis的增删改查入门实例,咱们发如今 mybatis-configuration.xml 的配置文件中,对数据库的配置都是硬编码在这个xml文件中,以下图,那么咱们如何改进这个写法呢?java

  

一、咱们将 数据库的配置语句写在 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

 

二、在  mybatis-configuration.xml 中加载db.properties文件并读取

1
2
3
4
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 文件mysql

注意:咱们也能够在<properties></properties>中手动增长属性sql

1
2
3
4
<!-- 加载数据库属性文件 -->
<properties resource= "db.properties" >
     <property name= "username"  value= "aaa" />
</properties>

  那么这个时候是读取的username 是以 db.properties 文件中的 root 为准,仍是以本身配置的 aaa 为准呢?数据库

咱们先看一段 properties 文件加载的源码mybatis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
   }
}

经过源码咱们能够分析读取优先级:

    一、在 properties 内部自定义的属性值第一个被读取app

    二、而后读取 resource 路径表示文件中的属性,若是有它会覆盖已经读取的属性;若是 resource 路径不存在,那么读取 url 表示路径文件中的属性,若是有它会覆盖第一步读取的属性值post

    三、最后读取 parameterType 传递的属性值,它会覆盖已读取的同名的属性ui

 

  前面两步好理解,第三步咱们能够举个例子来看:this

    咱们在 userMapper.xml 文件中进行模糊查询编码

1
2
3
4
<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 默认支持的别名

  

  

 

二、自定义别名  

  1、定义单个别名

    首先在全局配置文件 mybatis-configuration.xml 文件中添加以下配置:是在<configuration>标签下

1
2
3
4
<!-- 定义别名 -->
<typeAliases>
     <typeAlias type= "com.ys.po.User"  alias= "user" />
</typeAliases>

    第二步经过 user 引用

  

  2、批量定义别名

    在全局配置文件 mybatis-configuration.xml 文件中添加以下配置:是在<configuration>标签下

1
2
3
4
5
<!-- 定义别名 -->
<typeAliases>
     <!-- mybatis自动扫描包中的po类,自动定义别名,别名是类名(首字母大写或小写均可以,通常用小写) -->
     < package  name= "com.ys.po" />
</typeAliases>

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

相关文章
相关标签/搜索