Mybatis學習筆記:ResultMap作用(結果映射,一對多,多對多等)


1.兩大作用(配置結果類型): 

  • 解決數據表列名和javabean屬性不一致的問題
  • 提供一對一、一對多、多對多等高級映射

2.當實體類對象與數據庫字段不匹配時:

  • 方案1:使用sql語句as 起別名的方式修改查詢結果的名稱
  • 方案2:使用resultMap,完成數據庫字段與實體類屬性的映射。(可被多個sql語句共用)
<!--
解決列名和實體類屬性不匹配方法
1.  select  id,name ,age,sex,height,s_address as address  from  student_tb where id = #{id}
2.  使用 resultmap
     1.聲明resultMap標簽  在內部完成映射
     2.查詢結果使用resultMap="studentMap1" 應用聲明的resultMap的id
-->
    <!-- 定義一個 resultMap
    使用resultMap 完成 數據庫列名到 實體類的映射
    <id property="id" column="id"></id>  實體類屬性與數據表 主鍵列名 映射
    <result property="name" column="name"></result>  實體類屬性與 普通列名 映射
    -->
    <resultMap id="studentMap1" type="com.qfedu.entity.Student">
        <id property="id" column="id"></id>   <!-- 主鍵-->
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="height" column="height"></result>
        <result property="address" column="s_address"></result>

    </resultMap>

    <select id="findStudentById" resultMap="studentMap1" parameterType="int">
         select  id,name ,age,sex,height,s_address   from  student_tb where id = #{id}
    </select>

3.一對一:

使用一對一查詢時,在一個實體類中添加另一實體類屬性。用resultMap實現映射關系時,使用association連接,javaType為封裝的類型。

如下:在Score中添加Student屬性,以及getter和setter方法

映射關系

 <!--定義 resultType  來接收getAllScoreWithStudent2 查詢結果-->
    <!--
        一對一查詢
       <association 解決一對一問題
       property="student" 對應Score 中 student屬性
       column="studentid" 對應Score_tb 與 student_tb關聯的外鍵 列名,可以不寫
       javaType="Student"  將其他非Score 中的字段寫入Student 類
             <result property="name" column="name"></result> 寫入student對應的屬性
    -->
    <resultMap id="scoreMap1" type="Score">
        <id column="scoreid" property="scoreid"></id>
        <result column="couresename" property="couresename"></result>
        <result column="score" property="score"></result>
        <result column="studentid" property="studentid"></result>

        <association property="student" column="studentid" javaType="Student" >
            <id property="id" column="id"></id>
            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
            <result property="sex" column="sex"></result>
            <result property="height" column="height"></result>
            <result property="sAddress" column="s_address"></result>
        </association>
    </resultMap>

    <select id="getAllScoreWithStudent2" resultMap="scoreMap1">
         SELECT a.*,b.* FROM score_tb a  LEFT JOIN student_tb b ON a.studentid = b.id;
    </select>

3.一對多

一對多是在一個類中包含另一個類list的集合,在映射時,使用Collection,ofType為封裝類型。注意封裝類型與一對一不同

如下:在學生實體中。添加score集合

映射關系:

 <!--
    <collection  解決一對多問題

    property="scoreList"  將collection 組成的成績集合放置到 student對象中
    column="id"   根據相同 的學生id組成集合
    ofType="Score"  將學生id組成集合 每一個item 生成一個 Score
    -->
    <resultMap id="studentWithScoreMap" type="Student">
        <id column="id" property="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="height" column="height"></result>
        <result property="sAddress" column="s_address"></result>

        <collection property="scoreList" column="id" ofType="Score">
            <id property="scoreid" column="scoreid"></id>
            <result property="couresename" column="couresename"></result>
            <result property="score" column="score"></result>
            <result property="studentid" column="id"></result>
        </collection>

    </resultMap>

    <!-- 查詢所有的學生並包含成績-->
    <select id="findAllStudentWithScore" resultMap="studentWithScoreMap">
        SELECT  * FROM student_tb a LEFT JOIN score_tb b ON a.id = b.studentid
    </select>

4.多對多

表之間的多對多關系稍微復雜,需要一個中間表來表示

中間表有三個字段,其中兩個字段分別作為外鍵指向各自一方的主鍵,由此通過中間表來表示多對多關系,通過一個表聯合另一個中間表可以表示為一對多關系。

多對多,其實可以從一方到另一方表示為一對多關系,反之亦然

student_role_tb :  id,studentid, roieid

如:查找職位,並包含對應的學生信息,一個職位可能有很多學生,如組長,一對多關系

第一步: 在role實體中添加student屬性,

第二步:書寫映射關系,按一對多即可

 <resultMap id="roleWithStudent" type="Role">
        <id property="roleid" column="roleid"></id>
        <result property="rolename" column="rolename"></result>
<!--property  role表中的屬性-->
        <collection property="studentList" column="studentid" ofType="Student"> 
            <id column="id" property="id"></id>
            <result property="name" column="name"></result>
            <result property="age" column="age"></result>
            <result property="sex" column="sex"></result>
            <result property="height" column="height"></result>
            <result property="sAddress" column="s_address"></result>
        </collection>
    </resultMap>
    <select id="findAllRoleWithStudent" resultMap="roleWithStudent">
        select r.*,sr.studentid, s.* from role_tb r LEFT JOIN student_role_tb sr on r.roleid=sr.roleid  
        LEFT JOIN student_tb s on s.id = sr.studentid
    </select>

反之亦然,如:查找學生並包含職位信息,也是一對多關系

第一步: 在student實體中添加role屬性,

第二步:書寫映射關系,按一對多即可

<!--多對多中的一對多-->
    <resultMap id="studentWithRole" type="Student">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="height" column="height"></result>
        <result property="sAddress" column="s_address"></result>
        <collection property="roleList" column="roleid" ofType="Role">
        <id property="roleid" column="roleid"></id>
            <result property="rolename" column="rolename"></result>
        </collection>
    </resultMap>
    <select id="findAllStudentWithRole" resultMap="studentWithRole">
        select s.*,sr.studentid, r.* from student_tb s LEFT JOIN student_role_tb sr on s.id = sr.studentid
          LEFT JOIN role_tb r on r.roleid = sr.roleid;
    </select>

 


免責聲明!

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



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