ResultMap詳解
前言
MyBatis是基於“資料庫結構不可控”的思想建立的,也就是我們希望資料庫遵循第三正規化或BCNF,但實際事與願違,那麼結果集對映就是MyBatis為我們提供這種理想與現實間轉換的手段了,而resultMap就是結果集對映的配置標籤了。
在深入ResultMap標籤前,我們需要了解從SQL查詢結果集到JavaBean或POJO實體的過程。
從SQL查詢結果到領域模型實體
- 通過JDBC查詢得到ResultSet物件
- 遍歷ResultSet物件並將每行資料暫存到HashMap例項中,以結果集的欄位名或欄位別名為鍵,以欄位值為值
- 根據ResultMap標籤的type屬性通過反射例項化領域模型
- 根據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元素 ,用於設定普通欄位與領域模型屬性的對映關係
id、result語句屬性配置細節:
屬性 | 描述 |
---|---|
property | 需要對映到JavaBean 的屬性名稱。 |
column | 資料表的列名或者標籤別名。 |
javaType | 一個完整的類名,或者是一個類型別名。如果你匹配的是一個JavaBean,那MyBatis 通常會自行檢測到。然後,如果你是要對映到一個HashMap,那你需要指定javaType 要達到的目的。 |
jdbcType | 資料表支援的型別列表。這個屬性只在insert,update 或delete 的時候針對允許空的列有用。JDBC 需要這項,但MyBatis 不需要。如果你是直接針對JDBC 編碼,且有允許空的列,而你要指定這項。 |
typeHandler | 使用這個屬性可以覆寫型別處理器。這項值可以是一個完整的類名,也可以是一個類型別名。 |
示例2:通過建構函式構造領域模型
<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、一對一關係、一對多關係查詢
注意:在採用巢狀結果的方式查詢一對一、一對多關係時,必須要通過resultMap下的id或result標籤來顯式設定屬性/欄位對映關係,否則在查詢多條記錄時會僅僅返回最後一條記錄的情況。
association聯合
聯合元素用來處理“一對一”的關係。需要指定對映的Java實體類的屬性,屬性的javaType(通常MyBatis 自己會識別)。對應的資料庫表的列名稱。如果想覆寫的話返回結果的值,需要指定typeHandler。
不同情況需要告訴MyBatis 如何載入一個聯合。MyBatis 可以用兩種方式載入:
- select: 執行一個其它對映的SQL 語句返回一個Java實體型別。較靈活;
- resultsMap: 使用一個巢狀的結果對映來處理通過join查詢結果集,對映成Java實體型別。
例如,一個班級對應一個班主任。
首先定義好班級中的班主任 private TeacherEntity teacherEntity;
使用select實現聯合
例:班級實體類中有班主任的屬性,通過聯合在得到一個班級實體時,同時映射出班主任實體。
這樣可以直接複用在TeacherMapper.xml檔案中定義好的查詢teacher根據其ID的select語句。而且不需要修改寫好的SQL語句,只需要直接修改resultMap即可。
ClassMapper.xml檔案部分內容:
<resultMap type="ClassEntity" id="classResultMap">
<id property="classID" column="CLASS_ID" />
<result property="className" column="CLASS_NAME" />
<result property="classYear" column="CLASS_YEAR" />
<association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/>
</resultMap>
<select id="getClassByID" parameterType="String" resultMap="classResultMap">
SELECT * FROM CLASS_TBL CT
WHERE CT.CLASS_ID = #{classID};
</select>
TeacherMapper.xml檔案部分內容:
<resultMap type="TeacherEntity" id="teacherResultMap">
<id property="teacherID" column="TEACHER_ID" />
<result property="teacherName" column="TEACHER_NAME" />
<result property="teacherSex" column="TEACHER_SEX" />
<result property="teacherBirthday" column="TEACHER_BIRTHDAY"/>
<result property="workDate" column="WORK_DATE"/>
<result property="professional" column="PROFESSIONAL"/>
</resultMap>
<select id="getTeacher" parameterType="String" resultMap="teacherResultMap">
SELECT *
FROM TEACHER_TBL TT
WHERE TT.TEACHER_ID = #{teacherID}
</select>
使用resultMap實現聯合
與上面同樣的功能,查詢班級,同時查詢器班主任。需在association中新增resultMap(在teacher的xml檔案中定義好的),新寫sql(查詢班級表left join教師表),不需要teacher的select。
修改ClassMapper.xml檔案部分內容:
<resultMap type="ClassEntity" id="classResultMap">
<id property="classID" column="CLASS_ID" />
<result property="className" column="CLASS_NAME" />
<result property="classYear" column="CLASS_YEAR" />
<association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/>
</resultMap>
<select id="getClassAndTeacher" parameterType="String" resultMap="classResultMap">
SELECT *
FROM CLASS_TBL CT LEFT JOIN TEACHER_TBL TT ON CT.TEACHER_ID = TT.TEACHER_ID
WHERE CT.CLASS_ID = #{classID};
</select>
其中的teacherResultMap請見上面TeacherMapper.xml檔案部分內容中。
collection聚集
聚集元素用來處理“一對多”的關係。需要指定對映的Java實體類的屬性,屬性的javaType(一般為ArrayList);列表中物件的型別ofType(Java實體類);對應的資料庫表的列名稱;
不同情況需要告訴MyBatis 如何載入一個聚集。MyBatis 可以用兩種方式載入:
1. select: 執行一個其它對映的SQL 語句返回一個Java實體型別。較靈活;
2. resultsMap: 使用一個巢狀的結果對映來處理通過join查詢結果集,對映成Java實體型別。
例如,一個班級有多個學生。
首先定義班級中的學生列表屬性:private List<StudentEntity> studentList;
使用select實現聚集
用法和聯合很類似,區別在於,這是一對多,所以一般對映過來的都是列表。所以這裡需要定義javaType為ArrayList,還需要定義列表中物件的型別ofType,以及必須設定的select的語句名稱(需要注意的是,這裡的查詢student的select語句條件必須是外來鍵classID)。
ClassMapper.xml檔案部分內容:
<resultMap type="ClassEntity" id="classResultMap">
<id property="classID" column="CLASS_ID" />
<result property="className" column="CLASS_NAME" />
<result property="classYear" column="CLASS_YEAR" />
<association property="teacherEntity" column="TEACHER_ID" select="getTeacher"/>
<collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" select="getStudentByClassID"/>
</resultMap>
<select id="getClassByID" parameterType="String" resultMap="classResultMap">
SELECT * FROM CLASS_TBL CT
WHERE CT.CLASS_ID = #{classID};
</select>
StudentMapper.xml檔案部分內容:
<!-- java屬性,資料庫表字段之間的對映定義 -->
<resultMap type="StudentEntity" id="studentResultMap">
<id property="studentID" column="STUDENT_ID" />
<result property="studentName" column="STUDENT_NAME" />
<result property="studentSex" column="STUDENT_SEX" />
<result property="studentBirthday" column="STUDENT_BIRTHDAY" />
</resultMap>
<!-- 查詢學生list,根據班級id -->
<select id="getStudentByClassID" parameterType="String" resultMap="studentResultMap">
<include refid="selectStudentAll" />
WHERE ST.CLASS_ID = #{classID}
</select>
使用resultMap實現聚集
使用resultMap,就需要重寫一個sql,left join學生表。
<resultMap type="ClassEntity" id="classResultMap">
<id property="classID" column="CLASS_ID" />
<result property="className" column="CLASS_NAME" />
<result property="classYear" column="CLASS_YEAR" />
<association property="teacherEntity" column="TEACHER_ID" resultMap="teacherResultMap"/>
<collection property="studentList" column="CLASS_ID" javaType="ArrayList" ofType="StudentEntity" resultMap="studentResultMap"/>
</resultMap>
<select id="getClassAndTeacherStudent" parameterType="String" resultMap="classResultMap">
SELECT *
FROM CLASS_TBL CT
LEFT JOIN STUDENT_TBL ST
ON CT.CLASS_ID = ST.CLASS_ID
LEFT JOIN TEACHER_TBL TT
ON CT.TEACHER_ID = TT.TEACHER_ID
WHERE CT.CLASS_ID = #{classID};
</select>
其中的teacherResultMap請見上面TeacherMapper.xml檔案部分內容中。studentResultMap請見上面StudentMapper.xml檔案部分內容中。
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)