使用动态sql批量更新数据库中的值
直接上一个简单的小demo
mapper中的接口:
int updateStatus1(List<Integer> list);
xml中的sql:
<update id="updateStatus1">
update table set status = "1" where id in
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
#{item}
</foreach>
</update>
说明:该sql的功能主要是将表 table 中的 status 字段设置为 1,其过滤条件为 某天数据的id 存在于 一个list集合中,该集合的值为后台传递的数据。
该 sql 经过 mybatis 解析后会演变成如下形态:
update table set status = "1" where id in ( ?, ?, ?, .....)