写在前面
mysql是有巧妙的批量更新语句的
mysql大批量更新数据,update批量更新的方式
mybatis使用批量更新稍微优点复杂。
准备数据
代码
<update id="batchUpdate">
update `test_school`
<trim prefix="set" suffixOverrides=",">
<trim prefix="name = case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.name != null">
when id=#{item.id} then #{item.name}
</if>
</foreach>
</trim>
<trim prefix="disc = case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.disc != null">
when id=#{item.id} then #{item.disc}
</if>
</foreach>
</trim>
</trim>
where `id` in
<foreach collection="list" separator="," item="item" index="index" open="(" close=")">
#{item.id}
</foreach>
</update>
@ResponseBody
@RequestMapping("testMybatisBatchUpdate")
public void testMybatisBatchUpdate(){
List<TestSchool> list = new ArrayList<>();
list.add(new TestSchool("1", "青岛大学2", "美丽2"));
list.add(new TestSchool("2", "山东大学2", "大方2"));
list.add(new TestSchool("3", "济南大学2", "端庄2"));
System.out.println(schoolDao.batchUpdate(list));
}
执行
执行打印结果为3条。
数据库结果为
至此批量更新完毕。
更多推荐
mybatis实现数据的批量update更新-mysql
发布评论