Mybatis中 ResultMap的collection配置一對多使用詳解


resultMap可以將查詢到的多種數據,映射到一個符合要求的對象上。

 

  1.實現操作的基本流程

    調用Mapper接口的方法→→Mapper.xml文件中對應id的查詢語句(接口的方法名=查詢語句的id)→查詢語句配置resultMap屬性(屬性值是其對應ResultMap標簽的id)→在resultMap標簽中,配置查到的表的字段與對象的屬性的關系。

 

  2.主要總結一下collection的使用

    為了解決一對多類型的需求,使用collection標簽來處理其中的“多”,常表現為在類中的屬性為List<>

 

    2.1 resultMap部分:

      id - 唯一標識,不做贅述

      type - 映射的目標類型

        每一行都是一個 數據庫表中字段 與 對象屬性 的映射

        column - 數據庫表中的字段

        property - pojo對象的對應屬性

  <resultMap id="UserRolesMap" type="com.example.server.bean.User">
        <id column="u_id" property="userId" jdbcType="INTEGER"/>
        <result column="u_name" property="userName" jdbcType="VARCHAR"/>
                <!--collection部分見下,兩種類型就是把collection部分 分別填充在該處--!> 
    </resultMap>

    2.2 collection有兩種配置方式

      1.嵌套數據

      2.嵌套查詢

      兩種方式的主要區別在於collection,和sql語句處

      

      2.2.1.嵌套數據

//collection:
//在collection標簽(即”多“)的次級標簽中,直接配置List<>中對象的映射
//pojoA中"List<pojoB> pojoBList" 則property處填pojoBList 
<collection property="pojoA的一個集合屬性名" ofType="集合中的pojoB對象"/>
        <id column="表的字段(<>括號中的類的屬性所對應的)" jdbcType="字段類型(數據庫表的)" property="集合中對象的主鍵屬性(javabean)" />
    <result column="表的字段(可以為任意表中的)" jdbcType="字段類型(數據庫表的)" property="集合中的pojo對象的屬性" />  
</collection>

// 例子
//Role對象包括 roleId roleName字段 ,對應數據庫中表的字段是role_id role_name
<collection property="roleList" ofType="com.example.server.beans.Role"/>
        <id column="role_id" jdbcType="INTEGER" property="roleId" />
    <result column="role_Name" jdbcType="VARCHAR" property="roleName" />  
</collection>

//SQl:
//只有一段
<select id="它的id" resultMap="其語句查詢出的數據從依照那個resultMap來映射"> //該處寫的sql語句應是:查詢出來的數據,包括collection以及resultMap中的各個表字段(即每個column) </select>

      2.2.2嵌套查詢

//collection:
//不寫次級標簽,通過select實現嵌套查詢
//pojoA中"List<pojoB> pojoBList" 則property處填pojoBList 
//column 是傳入的參數(目前我也未搞懂,可以直接照傳入屬性值對應的表字段填寫)
<collection property="pojoA的一個集合屬性名" ofType="com.example.server.beans.PojoB" column="user_id" select="一個sql查詢的id(sql_id)"/>

//SQl:
//應當有兩段:
//1.是查詢resutMap的屬性(PojoA的其他屬性)的語句,其應將resultType換為resultMapper //2.即為下列查詢pojoB <select id = "sql_id" resultType = "com.example.server.beans.PojoB" parameterType = "INTEGER"> //該處寫的sql語句應是:用user_id對應的pojo屬性(即mapper接口中方法的傳參)來查詢出你所需要的PojoB的字段 的語句 </select>

(嵌套查詢的collection 也還有兩種,resultMap或者resultType 這里只寫了后者,前者可在下列代碼中的嵌套查詢collectionPlanA中見到)

  配上一個沒什么注釋的xml文件代碼--UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.server.mapper.UserMapper">
<!-- 嵌套數據,ResultMap --> <select id="getRolesByUserId" resultMap="UserRolesMap"> SELECT u.u_id, u.`u_name`, r.r_id, r.`r_name` FROM t_user u, t_role r, t_role_user ru WHERE ru.role_id = r.r_id AND ru.user_id = u.u_id AND u.u_id = #{userId} </select>
<!-- 嵌套數據,association或者collection中無select --> <resultMap id="UserRolesMap" type="com.example.server.bean.User"> <id column="u_id" property="userId" jdbcType="INTEGER"/> <result column="u_name" property="userName" jdbcType="VARCHAR"/> <!-- <result column="password" property="password" jdbcType="VARCHAR"/>--> <collection property="roleList" javaType="java.util.List" ofType="com.example.server.bean.Role" > <id column="r_id" property="roleId" jdbcType="INTEGER"/> <id column="r_name" property="roleName" jdbcType="VARCHAR"/> </collection> </resultMap> <!--嵌套查詢 ResultMap--> <select id="getRolesByUserId_1" resultMap="UserRolesMap_1"> SELECT u_id , `u_name` FROM t_user WHERE u_id = #{userId} </select> <!--collection嵌套查詢--> <resultMap id="UserRolesMap_1" type="com.example.server.bean.User"> <id column="u_id" property="userId"/> <result column="u_name" property="userName"/> <!-- collection一對多,property是List屬性的名字,ofType是List<>中的類型 column是傳入的參數--> <!-- select執行的是一個語句--> <collection property="roleList" ofType="com.example.server.bean.Role" column="u_id" select="findRoleListByUserId"/> </resultMap> <!-- 嵌套查詢collection plan A-->   <!-- 嵌套查詢RoleList select中的三個屬性(id resultMap parameterType)都不能少-->   <!--As后是對應pojo中的屬性名 有resultMap指定映射關系時就不用AS去對應屬性名了--> <select id="findRoleListByUserId" resultMap="getRoleListByUserIdMap" parameterType="INTEGER"> SELECT r.r_id , r.r_name FROM t_role r, t_role_user ru WHERE r.r_id = ru.role_id AND ru.user_id = #{userId} </select> <resultMap id="getRoleListByUserIdMap" type="com.example.server.bean.Role"> <id column="r_id" property="roleId"/> <result column="r_name" property="roleName"/> </resultMap> <!-- 嵌套查詢collection plan B-->   <!-- <select id="findRoleListByUserId" resultType="com.example.server.bean.Role" parameterType="INTEGER">-->   <!-- SELECT r.r_id AS roleId, r.r_name AS roleName-->   <!-- FROM t_role r, t_role_user ru-->   <!-- WHERE r.r_id = ru.role_id-->   <!-- AND ru.user_id = #{userId}-->   <!-- </select>-->

 

      錯誤歡迎指正,多多學習


免責聲明!

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



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