MyBatisPlus 中的 resultMap 使用

  1. 想让数据库返回 SkuItemSaleAttrVo 实体数据

    @ToString
    @Data
    public class SkuItemSaleAttrVo {
    
        private Long attrId;
        private String attrName;
        private List<AttrValueWithSkuIdVo> attrValues;
    }
    
    @Data
    @ToString
    public class AttrValueWithSkuIdVo {
    
        private String attrValue;
        private String skuIds;
    }
    
  2. 定义Mapper

    List<SkuItemSaleAttrVo> getSaleAttrsBySpuId(@Param("spuId") Long spuId);
    
  3. 定义Dao.xml

    <resultMap id="skuItemSaleAttrVo" type="com.feng.mall.product.vo.SkuItemSaleAttrVo">
        <result column="attr_id" property="attrId"></result>
        <result column="attr_name" property="attrName"></result>
        <collection property="attrValues" ofType="com.feng.mall.product.vo.AttrValueWithSkuIdVo">
            <result column="attr_value" property="attrValue"></result>
            <result column="sku_ids" property="skuIds"></result>
        </collection>
    </resultMap>
    
    <select id="getSaleAttrsBySpuId" resultMap="skuItemSaleAttrVo">
        select ssav.attr_id,
               ssav.attr_name,
               ssav.attr_value,
               group_concat(distinct info.sku_id) sku_ids
        from pms_sku_info info
                 left join pms_sku_sale_attr_value ssav
                           on ssav.sku_id = info.sku_id
        where info.spu_id = #{spuId}
        group by ssav.attr_id, ssav.attr_name, ssav.attr_value
    </select>
    
上一篇:MyBatis Plus简介与搭建


下一篇:Springboot + mybatisPLus