mybatis 中foreach中的坑

2021-10-20 21:41:18 字數 1042 閱讀 5541

我踩過一次坑,記錄一下,也發篇部落格來幫助同樣遇到這個問題的人

進入正題

有乙個需求是想通過傳入乙個字串陣列,然後通過goods_coding欄位匹配出不在此陣列內的所有商品。所以我使用foreach迴圈,**如下

test

="codings != null and codings.length>0"

>

and goods_coding not in

collection

="codings"

open

="("

separator

=","

close

=")"

item

="code"

index

="index"

>

#foreach

>

if>

正當我開開心心測試是出現了bug,就是無論傳的陣列如何,都無法排除陣列中的商品。經過幾個小時的問題排除,發現是因為foreach在遍歷時會自動加上一對單引號,比如傳入陣列[「jan」,「han」];那麼最終拼接將會變成(』 「jan」 『,』 「han」 '),由此就知道為什麼無法排除,解決方案也十分簡單,只需使用replace替換掉雙引號即可

test

="codings != null and codings.length>0"

>

and goods_coding not in

collection

="codings"

open

="("

separator

=","

close

=")"

item

="code"

index

="index"

>

replace(#,'"','')

foreach

>

if>

mybatis動態sql中foreach標籤的使用

foreach標籤主要用於構建in條件,他可以在sql中對集合進行迭代。如下 delete from user where id in 我們假如說引數為 int ids 那麼列印之後的sql如下 delete form user where id in 1,2,3,4,5 釋義 collection...

MyBatis中的foreach迴圈

mybatis動態sql中foreach標籤的使用 foreach標籤主要用於構建in條件,他可以在sql中對集合進行迭代。如下 delete from user where id in 我們假如說引數為 int ids 那麼列印之後的sql如下 delete form user where id ...

mybatis動態sql中foreach標籤的使用

foreach標籤主要用於構建in條件,他可以在sql中對集合進行迭代。如下 delete from user where id in 我們假如說引數為 int ids 那麼列印之後的sql如下 delete form user where id in 1,2,3,4,5 釋義 collection...