mybatis-plus 在 @Select註解使用 in 參數時的sql寫法

使用 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);

重點部分已經表紅色,可參考
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章