最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis中一對(duì)多(collection)和一對(duì)一(association)的組合查詢使用

 更新時(shí)間:2023年12月27日 09:28:45   作者:huangyaa729  
這篇文章主要介紹了Mybatis中一對(duì)多(collection)和一對(duì)一(association)的組合查詢使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Collection

  • collection : 一個(gè)復(fù)雜的類型關(guān)聯(lián),許多結(jié)果將映射為這種類型
  • property : 這是關(guān)聯(lián)的 JavaBean 中的屬性名, 在 RoleModel 中對(duì)應(yīng) private List<MenuModel> menus;
  • javaType : property 屬性對(duì)應(yīng)的集合類型
  • ofType : property 集合中的泛型,即定義時(shí)泛型所表示的具體類型
  • column : RoleModel 的 id ,作為參數(shù)傳入被調(diào)用的 Select 語(yǔ)句

用法,嵌套在其他的查詢說(shuō)sql中,寫(xiě)法 主要以下3種:

第一種:select 語(yǔ)句在其他dao.java對(duì)應(yīng)的 mapper.xml中

   <collection property="tProductVideos" ofType="com.pasture.mpi.provider.biz.model.auto.TProductVideo"
                        column="product_id"
                        select="com.mpi.biz.dao.cust.ProductDao.getProductList">
    </collection>

第二種:select語(yǔ)句在同一個(gè)mapper.xml中

(與第一種的區(qū)別在于select屬性的值不一樣)

 .....
  <collection property="tProductVideos" ofType="com.pasture.mpi.provider.biz.model.auto.TProductVideo"
                            column="product_id"
                            select="getProductList">
   </collection>
   ......
<resultMap id="BaseProduct" type="com.mpi.biz.model.auto.TProduct">
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="product_id" property="productId" jdbcType="INTEGER" />
        <result column="image_url" property="imageUrl" jdbcType="VARCHAR" />
        <result column="type" property="type" jdbcType="CHAR" />
    </resultMap>
    <select id="getProductImageList" resultMap="BaseProduct" parameterType="java.lang.Integer">
        SELECT i.id, i.product_id, i.image_url,i.type
        FROM t_product_image i
        WHERE i.product_id = #{productId}
    </select>

第三種,如果再一個(gè)查詢中可以直接查詢到所需要的數(shù)據(jù)

但是需要映射到該對(duì)象的屬性上,則可以使用該方式(建議使用上兩種,看起來(lái)更清晰):

<resultMap id="BaseRoleResultMap" type="cn.com.hellowood.springsecurity.model.RoleModel">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="is_active" property="isActive" jdbcType="BOOLEAN"/>
        <result column="description" property="description" jdbcType="VARCHAR"/>
        <result column="last_update_time" property="lastUpdateTime" jdbcType="TIMESTAMP"/>
        <collection property="menus" ofType="com.pasture.mpi.provider.biz.model.auto.TProductVideo"
                    javaType="java.util.ArrayList">
               <id column="id" property="id" jdbcType="INTEGER" />
            <result column="product_id" property="productId" jdbcType="INTEGER" />
            <result column="image_url" property="imageUrl" jdbcType="VARCHAR" />
            <result column="type" property="type" jdbcType="CHAR" />
        </collection>
    </resultMap>

<select id="getRoles" parameterType="java.lang.Integer" resultMap="BaseRoleResultMap">
    SELECT
        r.id,
        r.name,
        r.description,
        r.is_active,
        r.last_update_time,
        m.id,
        m.value,
        m.display_value,
        m.url,
        m.category,
        m.description,
        m.is_active,
        m.last_update_time
    FROM table1  r
        LEFT JOIN t_product p
            ON r.id = p.r_id
    WHERE r.id = #{roleId,jdbcType=INTEGER}
</select>

association總結(jié)

主要用到的屬性:

  • property:映射數(shù)據(jù)庫(kù)列的字段或?qū)傩浴?/li>
  • column:數(shù)據(jù)庫(kù)的列名或者列標(biāo)簽別名。與傳遞給resultSet.getString(columnName)的參數(shù)名稱相同。
  • javaType:完整java類名或別名(參考上面的內(nèi)置別名列表)。如果映射到一個(gè)JavaBean,那MyBatis 通常會(huì)自行檢測(cè)到。然而,如果映射到一個(gè)HashMap,那您應(yīng)該明確指定javaType 來(lái)確保所需行為。
  • resultMap:一個(gè)可以映射聯(lián)合嵌套結(jié)果集到一個(gè)適合的對(duì)象視圖上的ResultMap。這是一個(gè)替代的方式去調(diào)用另一個(gè)select語(yǔ)句。

association查詢與主查詢一般寫(xiě)在一個(gè)文件中,如下:

第一種:不另外寫(xiě)查詢語(yǔ)句

(1)把返回的屬性提取出來(lái)

<association property="product" resultMap="ResultMapWithBLOBs"/>

(2)糅合在一起

  <association property="category" javaType="com.provider.biz.model.auto.TCategory">
            <id column="category_id" property="categoryId" jdbcType="INTEGER"/>
            <result column="c_name" property="name" jdbcType="VARCHAR"/>
            <result column="image_url" property="imageUrl" jdbcType="VARCHAR"/>
            <result column="c_status" property="status" jdbcType="CHAR"/>
  </association>

第二種:另外寫(xiě)查詢語(yǔ)句

<resultMap id="testMap"  type="xxx" >
        <id property="id" column="id" jdbcType="DECIMAL" />
        <result property="startDateStr" column="startDateStr"  jdbcType="VARCHAR" />
        <result property="endDateStr" column="endDateStr"  jdbcType="VARCHAR" />
        <association property="tableEntity" column="table_a_id" select="selectById"></association>
</resultMap>

<select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select * from Table_XXX
where a_id=#{aId}  (注:這里的aId即為上述傳的參數(shù)值table_a_id,只傳一個(gè)參數(shù)時(shí)名稱可以不一樣)
</select>

第三種:與collection一樣

提到外面其他的dao接口中,此處省略…

補(bǔ)充:

association的使用要特別注意下,數(shù)據(jù)映射時(shí)會(huì)出現(xiàn)異常,再一次的項(xiàng)目中就出現(xiàn)過(guò)實(shí)際有5條數(shù)據(jù),但返回到service層時(shí)只有1條數(shù)據(jù),這一條數(shù)據(jù)各個(gè)部分的信息還不對(duì)應(yīng),通過(guò)打印mysql執(zhí)行語(yǔ)句和逐條排查,發(fā)現(xiàn)是association標(biāo)簽的問(wèn)題;

切記,association使用時(shí)<resultMap id="testMap"  type="xxx" >中一定要有 <id>標(biāo)簽,否則會(huì)出現(xiàn)返回的數(shù)據(jù)與實(shí)際查詢結(jié)果不一致的問(wèn)題

多參數(shù)傳遞

當(dāng)需要傳遞多個(gè)值時(shí),寫(xiě)法如下:

column= “{prop1=col1,prop2=col2}

接收的類型為 parameterType=“java.util.Map”,這里的參數(shù)使用時(shí)名稱要保持一致;

例:

   <resultMap id="testMap"  type="xxx" >
        <id property="id" column="id" jdbcType="DECIMAL" />
        <result property="startDateStr" column="startDateStr"  jdbcType="VARCHAR" />
        <result property="endDateStr" column="endDateStr"  jdbcType="VARCHAR" />
        <association property="tableEntity" column="{reqId=id,endDate=endDateStr,startDate=startDateStr}" select="selectById">                     </association>
</resultMap>

<select id="selectById" parameterType="java.util.Map" resultMap="BaseResultMap">

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

霍山县| 车险| 兴山县| 安国市| 西乌珠穆沁旗| 澄迈县| 安宁市| 灵宝市| 来宾市| 云安县| 兴义市| 海门市| 米脂县| 荥经县| 瓮安县| 喀喇沁旗| 平遥县| 景泰县| 塔城市| 泾川县| 华安县| 旬邑县| 澎湖县| 台山市| 榆树市| 邳州市| 永仁县| 雷州市| 宜都市| 南部县| 陕西省| 连平县| 峨眉山市| 嘉峪关市| 沙湾县| 广水市| 平阳县| 密山市| 什邡市| 江门市| 渭源县|