前言
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)