網(wǎng)站建設(shè)設(shè)計(jì)解決方案網(wǎng)推拉新app推廣接單平臺(tái)
foreach標(biāo)簽簡(jiǎn)介
實(shí)踐
demo1
簡(jiǎn)單的一個(gè)批量更新,這里傳入了一個(gè)List類(lèi)型的集合作為參數(shù),拼接到 in 的后面 ,來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的批量更新
<update id="updateVislxble" parameterType="java.util.List">update models set visible =0where llm_idIN<foreach collection="allVisible" item="visible" open="(" separator="," close=")">#{visible}</foreach></update>
- collection=“allVisible”,allVisible代表傳入的集合
- item=“visible” ,visible代表集合的每一個(gè)元素
- open=“(” 代表以( 開(kāi)頭
- separator=“,” 代表以“,”分隔,
- close=“)” 代表以“)”結(jié)束
本質(zhì)上這里是個(gè)拼接,所有要保證這里list不為空,不然會(huì)報(bào)錯(cuò)
demo2
這里還是個(gè)批量更新,但是傳入的參數(shù)多了起來(lái),這里是的入?yún)⑹莻€(gè)map,并且里面這個(gè)status是一個(gè)List類(lèi)型的
<select id="getModelEvaluateTaskPage" parameterType="java.util.Map" resultType="com.test.test">select * from evaluate_tasks t<where><if test="status !=null">and t.status in<foreach item="item" index="index" collection="status"open="(" separator="," close=")">#{item}</foreach></if><if test="taskName !=null and taskName!=''">and t.task_name like concat('%',#{taskName},'%')</if><if test="startTime !=null and endTime!=null">and t.created_time between #{startTime} and #{endTime}</if>and t.user_id = #{userId}</where>order by t.created_time ${sort}</select>
demo3
這里的場(chǎng)景傳入了一個(gè)list,里面是多個(gè)對(duì)象,根據(jù)對(duì)象的屬性A去更新屬性B,這里額外使用了 case when then
<update id="bitchUpdateStatueById" parameterType="java.util.List">UPDATE evaluate_tasksSET status = CASE service_name<foreach collection="updates" item="update" separator=" ">WHEN #{update.serviceName} THEN #{update.status}</foreach>END,updated_time = CASE service_name<foreach collection="updates" item="update" separator=" ">WHEN #{update.serviceName} THEN #{update.endTime}</foreach>ENDWHERE service_name IN<foreach collection="updates" item="update" open="(" separator="," close=")">#{update.serviceName}</foreach></update>