Mybaits的中的對象映射(包含僅有基本數據類型的屬性的和對象類型的屬性的)


轉:https://blog.csdn.net/cjt20100/article/details/46547617.

1 constructor – 用來將結果反射給一個實例化好的類的構造器
    a) idArg – ID 參數;將結果集標記為ID,以方便全局調用
     b) arg –反射到構造器的通常結果
2. id – ID 結果,將結果集標記為ID,以方便全局調用
3. result – 反射到JavaBean 屬性的普通結果
4. association – 復雜類型的結合;多個結果合成的類型
a) nested result mappings – 幾resultMap 自身嵌套關聯,也可以引用到一個其它上
5. collection –復雜類型集合a collection of complex types
6. nested result mappings – resultMap 的集合,也可以引用到一個其它上
7. discriminator – 使用一個結果值以決定使用哪個resultMap
a) case – 基本一些值的結果映射的case 情形
i. nested result mappings –一個case 情形本身就是一個結果映射,因此也可以包括一些相同的元素,也可以引用一個外部resultMap。


1)id:為主鍵映射 result:為普通字段的映射

<resultMap type="liming.student.manager.data.model.StudentEntity" id="studentResultMap">  
    <id  property="studentId"        column="STUDENT_ID" javaType="String" jdbcType="VARCHAR"/>  
    <result property="studentName"       column="STUDENT_NAME" javaType="String" jdbcType="VARCHAR"/>  
    <result property="studentSex"        column="STUDENT_SEX"  javaType="int" jdbcType="INTEGER"/>  
    <result property="studentBirthday"   column="STUDENT_BIRTHDAY"  javaType="Date" jdbcType="DATE"/>  
    <result property="studentPhoto"  column="STUDENT_PHOTO" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" />  
</resultMap> 
id和result的屬性配置:
屬性

描述

 

property

需要映射到JavaBean 的屬性名稱。

 

column

數據表的列名或者標簽別名。

 

javaType

一個完整的類名,或者是一個類型別名。如果你匹配的是一個JavaBean,那MyBatis 通常會自行檢測到。然后,如果你是要映射到一個HashMap,那你需要指定javaType 要達到的目的。

 

jdbcType

數據表支持的類型列表。這個屬性只在insert,update 或delete 的時候針對允許空的列有用。JDBC 需要這項,但MyBatis 不需要。如果你是直接針對JDBC 編碼,且有允許空的列,而你要指定這項。

 

typeHandler

使用這個屬性可以覆寫類型處理器。這項值可以是一個完整的類名,也可以是一個類型別名。

2)constructor
我們使用id、result時候,需要定義java實體類的屬性映射到數據庫表的字段上。這個時候是使用JavaBean實現的。當然我們也可以使用實體類的構造方法來實現值的映射,這個時候是通過構造方法參數的書寫的順序來進行賦值的。
        使用construcotr功能有限(例如使用collection級聯查詢)。
        上面使用id、result實現的功能就可以改為:

<resultMap type="StudentEntity" id="studentResultMap" >  
    <constructor>  
        <idArg javaType="String" column="STUDENT_ID"/>  
        <arg javaType="String" column="STUDENT_NAME"/>  
        <arg javaType="String" column="STUDENT_SEX"/>  
        <arg javaType="Date" column="STUDENT_BIRTHDAY"/>  
    </constructor>  
</resultMap> 
public StudentEntity(String studentID, String studentName, String studentSex, Date studentBirthday){  
    this.studentID = studentID;  
    this.studentName = studentName;  
    this.studentSex = studentSex;  
    this.studentBirthday = studentBirthday;  

3)association:處理一對一的關系
聯合元素用來處理“一對一”的關系。需要指定映射的Java實體類的屬性,屬性的javaType(通常MyBatis 自己會識別)。對應的數據庫表的列名稱。如果想覆寫的話返回結果的值,需要指定typeHandler。
不同情況需要告訴MyBatis 如何加載一個聯合。MyBatis 可以用兩種方式加載:

1. select: 執行一個其它映射的SQL 語句返回一個Java實體類型。較靈活;
2. 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文件部分內容中。
4)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文件部分內容中。
5)discriminator鑒別器

有時一個單獨的數據庫查詢也許返回很多不同(但是希望有些關聯)數據類型的結果集。鑒別器元素就是被設計來處理這個情況的,還有包括類的繼承層次結構。鑒別器非常容易理解,因為它的表現很像Java語言中的switch語句。

定義鑒別器指定了column和javaType屬性。列是MyBatis查找比較值的地方。JavaType是需要被用來保證等價測試的合適類型(盡管字符串在很多情形下都會有用)。

下面這個例子為,當classId為20000001時,才映射classId屬性。

<resultMap type="liming.student.manager.data.model.StudentEntity" id="resultMap_studentEntity_discriminator">  
    <id  property="studentId"        column="STUDENT_ID" javaType="String" jdbcType="VARCHAR"/>  
    <result property="studentName"       column="STUDENT_NAME" javaType="String" jdbcType="VARCHAR"/>  
    <result property="studentSex"        column="STUDENT_SEX"  javaType="int" jdbcType="INTEGER"/>  
    <result property="studentBirthday"   column="STUDENT_BIRTHDAY"  javaType="Date" jdbcType="DATE"/>  
    <result property="studentPhoto"  column="STUDENT_PHOTO" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler" />  
    <result property="placeId"           column="PLACE_ID" javaType="String" jdbcType="VARCHAR"/>  
    <discriminator column="CLASS_ID" javaType="String" jdbcType="VARCHAR">  
        <case value="20000001" resultType="liming.student.manager.data.model.StudentEntity" >  
            <result property="classId" column="CLASS_ID" javaType="String" jdbcType="VARCHAR"/>  
        </case>  
    </discriminator>  
</resultMap> 


————————————————
版權聲明:本文為CSDN博主「caonjiantong」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/cjt20100/article/details/46547617


免責聲明!

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



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