您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“mybatis分割字符串并循環實現in多個參數的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“mybatis分割字符串并循環實現in多個參數的示例分析”這篇文章吧。
mybatis xml代碼:
<select id="selectInXh" resultMap="BaseResultMap" parameterType="java.lang.String"> select * from carinfo where xh in <if test="param1 != null and param1 != ''"> <foreach item="item" index="index" collection="param1.split(',')" open="(" separator="," close=")"> #{item} </foreach> </if> </select>
mybatis sql打印:
==> Preparing: select * from carinfo where xh in ( ? , ? ) ==> Parameters: 1(String), 2(String)
List<AnalysisInfo> listInfo(@Param("orderIds") List<Integer> orderIds);
我這里對參數重命名為"orderIds",所以下面foreach中collection="orderIds",如果未重命名則foreach中collection="list"
<select id="listInfo" resultType="com.ieou.retail.module.H5.dto.AnalysisInfo"> select materials_name as materialsName,sum(num) as totalNum, sum(price) as totalSale from sales_order_detail where shipment_result = 'SUCCESS' and refunds_time is null and sales_order_id in <foreach collection="orderIds" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> group by materials_id order by totalNum desc limit 5 </select>
List<AnalysisInfo> listInfo(Long[] orderIds);
如果參數類型是Array則collection屬性為array
<select id="listInfo" resultType="com.ieou.retail.module.H5.dto.AnalysisInfo"> select materials_name as materialsName,sum(num) as totalNum, sum(price) as totalSale from sales_order_detail where shipment_result = 'SUCCESS' and refunds_time is null and sales_order_id in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> group by materials_id order by totalNum desc limit 5 </select>
List<AnalysisInfo> listInfo(List<Integer> orderIds, Integer num);
這種情況下傳參要使用Map方式,這樣在collection屬性可以指定名稱
Map<String, Object> params = new HashMap<>(); params.put("orderIds",orderIds); params.put("num",num); List<AnalysisInfo> listInfo(params);
XML如下:
<select id="listInfo" resultType="com.ieou.retail.module.H5.dto.AnalysisInfo"> select materials_name as materialsName,sum(num) as totalNum, sum(price) as totalSale from sales_order_detail where shipment_result = 'SUCCESS' and refunds_time is null and num = #{num} and sales_order_id in <foreach collection="orderIds" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> group by materials_id order by totalNum desc limit 5 </select>
以上是“mybatis分割字符串并循環實現in多個參數的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。