测试代码
List<Info> infos = new ArrayList<>();
for (int i = 1; i < 1000; i++) {
infos.add(new Info(String.valueOf(i), String.valueOf(i), i + "0", new Time(System.currentTimeMillis()), new Time(System.currentTimeMillis())));
}
long begin = System.currentTimeMillis();
System.err.println("数据生成完毕,准备修改 使用CASE WHEN 语法:");
System.err.println(infoMapper.updateBatch(infos));
// System.out.println(infos);
System.err.printf("总用时:%s ms\n", System.currentTimeMillis() - begin);
注意 MySQL 要支持执行多条SQL语句请在url链接中加入配置:url: jdbc:mysql://localhost:3306/library?allowMultiQueries=true
使用case when 批量更新
<update id="updateBatch">-- noinspection SqlNoDataSourceInspectionForFile
UPDATE info
SET seat = CASE card
<foreach collection="list" item="item" separator=" ">
WHEN #{item.card} THEN #{item.seat}
</foreach>
ELSE seat
END
WHERE card IN
<foreach collection="list" item="item" open="(" close=")" separator=",">
#{item.card}
</foreach>
</update>
使用 普通foreach 循环更新
<update id="updateBatchByForEach">
<foreach collection="list" item="item">
UPDATE info
set seat = #{item.seat}
where card = #{item.card};
</foreach>
</update>
1万条数据测试
4万条数据