MyBatis魔法堂:ResultMap詳解


一、前言                               

  MyBatis是基於“數據庫結構不可控”的思想建立的,也就是我們希望數據庫遵循第三范式或BCNF,但實際事與願違,那么結果集映射就是MyBatis為我們提供這種理想與現實間轉換的手段了,而resultMap就是結果集映射的配置標簽了。

 

二、從SQL查詢結果到領域模型實體                  

  在深入ResultMap標簽前,我們需要了解從SQL查詢結果集到JavaBean或POJO實體的過程。

  1. 通過JDBC查詢得到ResultSet對象

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

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

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

 

三、ResultMap標簽                          

  1. 屬性說明

     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魔法堂:即學即用篇

     注意:在采用嵌套結果的方式查詢一對一、一對多關系時,必須要通過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字段值為4、5、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) 

 

四、總結                                  

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

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

 

五、參考                                  

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

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM