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

  上一篇博客咱們介紹了mybatis的增刪改查入門實例,咱們發如今 mybatis-configuration.xml 的配置文件中,對數據庫的配置都是硬編碼在這個xml文件中,以下圖,那麼咱們如何改進這個寫法呢?java

  

一、咱們將 數據庫的配置語句寫在 db.properties 文件中

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=root

 

二、在  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 文件mysql

注意:咱們也能夠在<properties></properties>中手動增長屬性sql

<!-- 加載數據庫屬性文件 -->
<properties resource="db.properties">
	<property name="username" value="aaa"/>
</properties>

  那麼這個時候是讀取的username 是以 db.properties 文件中的 root 爲準,仍是以本身配置的 aaa 爲準呢?數據庫

咱們先看一段 properties 文件加載的源碼mybatis

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 表示路徑文件中的屬性,若是有它會覆蓋第一步讀取的屬性值ui

    三、最後讀取 parameterType 傳遞的屬性值,它會覆蓋已讀取的同名的屬性this

 

  前面兩步好理解,第三步咱們能夠舉個例子來看:編碼

    咱們在 userMapper.xml 文件中進行模糊查詢url

  	<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>標籤下

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

    第二步經過 user 引用

  

  2、批量定義別名

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

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

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

相關文章
相關標籤/搜索