在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操作也可以。