使用 in 处理方法:
1、使用xml 方式 (不讲解)
2、使用 foreach 读取in 参数(如下)

原sql

正常sql 如下使用了in参数,如果 mybatis在 @Select 注解中使用in ,无法使用List 传递参数,
当然直接传递string参数也是不对的,逗号分隔参数会被当成1个参数

select 
spu.spu_category_id,spu.name ,spu.desc ,spu.specs_num  ,spu.spu_type ,spu.spu_img_url, spu.is_spec , 
sku.*
from trade_spu spu,trade_sku sku
where sku.deleted=0  
and spu.id=sku.spu_id 
and spu.id in("1236120201049714690") and sku.id in("1236120206250651649")

在这里插入图片描述

处理后的sql

    @Select("<script>" +
            "select \n" +
            "spu.spu_category_id,spu.name ,spu.desc ,spu.specs_num  ,spu.spu_type ,spu.spu_img_url, spu.is_spec , \n" +
            "sku.*\n" +
            "from trade_spu spu,trade_sku sku\n" +
            "where sku.deleted=0  \n" +
            "and spu.id=sku.spu_id \n" +
            "and spu.id in  " +
            "   <foreach item='item' index='index' collection='spuIds' open='(' separator=',' close=')'>" +
            "       #{item}" +
            "   </foreach>" +
            " and sku.id in" +
            "    <foreach item='item' index='index' collection='skuIds' open='(' separator=',' close=')'>" +
            "       #{item}" +
            "    </foreach>" +
            "</script>"
    )
    List<RpcSpuDetailVo> getSpuDetail(@Param("spuIds") List<String> spuIds, @Param("skuIds") List<String> skuIds);

重点部分已经表红色,可参考
在这里插入图片描述

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐