MyBatis魔法堂:ResultMap詳解

1、前言                               html

  MyBatis是基於「數據庫結構不可控」的思想創建的,也就是咱們但願數據庫遵循第三範式或BCNF,但實際事與願違,那麼結果集映射就是MyBatis爲咱們提供這種理想與現實間轉換的手段了,而resultMap就是結果集映射的配置標籤了。java

 

2、從SQL查詢結果到領域模型實體                  數據庫

  在深刻ResultMap標籤前,咱們須要瞭解從SQL查詢結果集到JavaBean或POJO實體的過程。app

  1. 經過JDBC查詢獲得ResultSet對象函數

  2. 遍歷ResultSet對象並將每行數據暫存到HashMap實例中,以結果集的字段名或字段別名爲鍵,以字段值爲值post

  3. 根據ResultMap標籤的type屬性經過反射實例化領域模型this

  4. 根據ResultMap標籤的type屬性和id、result等標籤信息將HashMap中的鍵值對,填充到領域模型實例中並返回spa

 

3、ResultMap標籤                          .net

  1. 屬性說明code

     id屬性 ,resultMap標籤的標識。

     type屬性 ,返回值的全限定類名,或類型別名。

     autoMapping屬性 ,值範圍true(默認值)|false, 設置是否啓動自動映射功能,自動映射功能就是自動查找與字段名小寫同名的屬性名,並調用setter方法。而設置爲false後,則須要在`resultMap`內明確註明映射關係纔會調用對應的setter方法。

  2. 基本做用:創建SQL查詢結果字段與實體屬性的映射關係信息

      示例1:經過setter構造領域模型

public class EStudent{
  private long id;
  private String name;
  private int age;
  // getter,setter方法

  /**
   * 必須提供一個無參數的構造函數
   */
  public EStudent(){}
}
<select id="getStudent" resultMap="getStudentRM">
  SELECT ID, Name, Age
    FROM TStudent
</select>
<resultMap id="getStudentRM" type="EStudnet">
  <id property="id" column="ID"/>
  <result property="studentName" column="Name"/>
  <result property="studentAge" column="Age"/>
</resultMap>

  子元素說明:

     id元素 ,用於設置主鍵字段與領域模型屬性的映射關係

     result元素 ,用於設置普通字段與領域模型屬性的映射關係

  示例2:經過構造函數構造領域模型

public class EStudent{
  private long id;
  private String name;
  private int age;
  // getter方法
  public EStudent(long id, String name, int age){
    this.id = id;
    this.name = name;
    this.age = age;
  }
}
<select id="getStudent" resultMap="getStudentRM">
  SELECT ID, Name, Age
    FROM TStudent
</select>
<resultMap id="getStudentRM" type="EStudnet">
  <constructor>
    <idArg column="ID" javaType="_long"/>
    <arg column="Name" javaType="String"/>
    <arg column="Age" javaType="_int"/>
  </constructor>
</resultMap>

   子元素說明:

      constructor元素 ,指定使用指定參數列表的構造函數來實例化領域模型。注意:其子元素順序必須與參數列表順序對應

                                 idArg子元素 ,標記該入參爲主鍵

                                 arg子元素 ,標記該入參爲普通字段(主鍵使用該子元素設置也是能夠的)

  3. 一對一關係、一對多關係查詢請參考《MyBatis魔法堂:即學即用篇

     注意:在採用嵌套結果的方式查詢一對1、一對多關係時,必需要經過resultMap下的id或result標籤來顯式設置屬性/字段映射關係,不然在查詢多條記錄時會僅僅返回最後一條記錄的狀況。

  4. 動態映射關係信息

    經過 discriminator子元素 (鑑別器)能夠實現動態映射關係信息的設置。具體示例以下:

public class EStudent{
  private long id;
  private String name;
  private String juniorHighSchool;
  private String seniorHighSchool;
  private int during; // 在本校就讀時間
  // getter,setter方法

  /**
   * 必須提供一個無參數的構造函數
   */
  public EStudent(){}
}

情景:查詢學生信息的seniorHighSchool信息,若就讀時間during字段值爲四、五、6時,則以juniorHighSchool字段做所爲seniorHighSchool信息。

<select id="getStundent" resultMap="rm">
  SELECT ID, Name, JuniorHighSchool, SeniorHighSchool, during
    FROM TStudent
</select>
<resultMap id="rm" type="EStudent">
  // 若不加這句,則當將juniorHighSchool賦予給seniorHighSchool屬性時,juniorHighSchool屬性將爲null
  <result column="juniorHighSchool" property="juniorHighSchool"/>

  <discriminator column="during" javaType="_int">
    // 形式1:經過resultType設置動態映射信息
    <case value="4" resultType="EStudent">
      <result column="juniorHighSchool" property="seniorHighSchool"/>
    </case>

   // 形式2: 經過resultMap設置動態映射信息
   <case value="5" resultMap="dynamicRM"/>
   <case value="6" resultMap="dynamicRM"/>
  </discriminator>
</resultMap>
<resultMap id="dynamicRM" type="EStudent">
  <result column="juniorHighSchool" property="seniorHighSchool"/>
</resultMap>

  注意:上面關於 discriminator子元素 的 case元素 的 resultType屬性 和 resultMap元素 的 type屬性 ,均不是直指返回的領域模型類型,而是指定根據判斷條件後獲得映射關係,可經過 id子元素 和 result子元素 重寫映射關係。

  5.  id元素,result元素,idArg元素,arg元素,discriminator元素的共同屬性

        javaType屬性 :Java類的全限定名,或別名

      jdbcType屬性 :JDBC類型, JDBC類型爲CUD操做時列可能爲空時進行處理

        typeHandler屬性 :指定類型處理器的全限定類名或類型別名

        column屬性 :指定SQL查詢結果的字段名或字段別名。將用於JDBC的 resultSet.getString(columnName) 

 

4、總結                                  

  掌握上述內容,那麼在寫一對一關係、一對多關係查詢時就更有把握了哦!

  尊重原創,轉載請註明來自:http://www.cnblogs.com/fsjohnhuang/p/4076592.html  ^_^肥仔John

 

5、參考                                  

http://blog.csdn.net/rootsuper/article/details/8542236

http://limingnihao.iteye.com/blog/781878

相關文章
相關標籤/搜索