我们来先看一下业务是怎样的:
我在库中查出了很多个人的planId,进行过一系列的处理之后,需要把这一堆planId对应的is_query字段更改为1,怎么做呢?一种方法是用for循环,一个一个的更新planId的is_query字段,拜托,2020年了,连白菜都涨价了,就不能不用for循环吗?
看一下接口该怎么写:
void updateQueryIdNew(@Param("planIdList") List<String> planIdList);
以下是Mapper:
< 这个符号是<,这涉及到一个知识点---->mybatis通配符,详情见我的另一篇博客:
<update id="updateQueryIdNew" parameterType="java.lang.String">
UPDATE tid_schedule_detail
SET
is_query = 1
WHERE
<foreach collection="planIDList" item="item" index="index" open="" close="" separator="or">
plan_id = #{item}
</foreach>
AND
plan_check_time < NOW()
</update>
可以看下执行的效果:
还可以有另外一种写法,其实也就是换了一下foreach的位置:
<update id="updateQueryIdNew" parameterType="java.lang.String">
<foreach collection="PlanIDList" item="item" index="index" open="" close="" separator=",">
UPDATE tid_schedule_detail
SET
is_query = 1
WHERE
plan_id = #{item}
AND
plan_check_time < NOW()
</foreach>
</update>
执行效果:
忘了截图了哈哈,大概是这个样子:plan_id='13425' or '23456' or '59832' or '24457' ……