mybatis分割字符串并循環(huán),實(shí)現(xiàn)in多個(gè)參數(shù)的操作
mybatis分割字符串并循環(huán),實(shí)現(xiàn)in多個(gè)參數(shù)
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打?。?/p>
==> Preparing: select * from carinfo where xh in ( ? , ? ) ==> Parameters: 1(String), 2(String)
mybatis多參數(shù)使用方法且其中有的參數(shù)是多個(gè)值使用in查詢
1.當(dāng)只有一個(gè)參數(shù)時(shí)且參數(shù)類型是List
List<AnalysisInfo> listInfo(@Param("orderIds") List<Integer> orderIds);
我這里對(duì)參數(shù)重命名為"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>
2. 當(dāng)只有一個(gè)參數(shù)時(shí)且參數(shù)類型是Array
List<AnalysisInfo> listInfo(Long[] orderIds);
如果參數(shù)類型是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>
3.注意當(dāng)查詢的參數(shù)有多個(gè)時(shí),例如
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>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Spring?Security?捕獲?filter?層面異常返回我們自定義的內(nèi)容
Spring?的異常會(huì)轉(zhuǎn)發(fā)到?BasicErrorController?中進(jìn)行異常寫入,然后才會(huì)返回客戶端。所以,我們可以在?BasicErrorController?對(duì)?filter異常進(jìn)行捕獲并處理,下面通過本文給大家介紹Spring?Security?捕獲?filter?層面異常,返回我們自定義的內(nèi)容,感興趣的朋友一起看看吧2022-05-05Spring Cloud Gateway重試機(jī)制原理解析
這篇文章主要介紹了Spring Cloud Gateway重試機(jī)制原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08使用WebUploader實(shí)現(xiàn)上傳文件功能(一)
這篇文章主要為大家詳細(xì)介紹了使用WebUploader實(shí)現(xiàn)上傳文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01