mybatis实用操作—— @MapKey

在mybatis中,可以将list集合转成map返回。用到注解:

 @MapKey("wiKey")
    Map<String,WarehouseInventoryLogMapVO> summaryCurrentStore(@Param("storeId")Long storeId,
                                                               @Param("customerId") Long customerId,
                                                               @Param("skuIdSet") Set<Long> skuIdSet);
<select id="summaryCurrentStore" resultMap="inventoryLogCollect" >
        select
        concat(customer_id,'-',store_id,'-',sku_id) wiKey,
        sum(total_store) changeBeforeTotal,
        sum(occ_store) changeBeforeOcc,
        sum(frozen_store) changeBeforeFrozen,
        sum(apply_store) changeBeforeApply,
        store_id storeId,
        customer_id customerId,
        sku_id skuId
        from warehouse_inventory
        <where>
            store_id = #{storeId,jdbcType=BIGINT}
            and customer_id = #{customerId,jdbcType=BIGINT}
            <if test="skuIdSet.size() > 0">
                and sku_id in
                <foreach collection="skuIdSet" close=")" open="(" separator="," item="item" index="index">
                    #{item,jdbcType=BIGINT}
                </foreach>
            </if>
        </where>
        group by customer_id,store_id,sku_id

    </select>


<resultMap id="inventoryLogCollect" type="com.runlion.sat.wms.vo.WarehouseInventoryLogMapVO">
        <result column="id" property="id"/>
        <result column="wiKey" property="wiKey"/>
        <result column="changeBeforeTotal" property="changeBeforeTotal"/>
        <result column="changeBeforeApply" property="changeBeforeApply"/>
        <result column="changeBeforeOcc" property="changeBeforeOcc"/>
        <result column="changeBeforeFrozen" property="changeBeforeFrozen"/>
        <result column="storeId" property="storeId"/>
        <result column="customerId" property="customerId"/>
        <result column="skuId" property="skuId"/>
    </resultMap>

如果不加 @MapKey("wiKey"),就会报错;

java.lang.ClassCastException: com.runlion.sat.wms.vo.WarehouseInventoryLogMapVO cannot be cast to java.util.Map。

其实直接返回List<WarehouseInventoryLogMapVO>也可以,然后实用jdk8流的map操作也可以。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值