关于批量更新,方式有很多,可以使用batch进行批处理,也可以直接自己使用jdbc进行批处理,今天我们要写的是mybatis 的语法组装成批处理的方式:
所用到的表结构如下:
CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`no` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
数据如下:
借口方法:
void uptateTable(@Param("lists") List<Map<String,Object>> lists); void uptateTable2(@Param("lists")List<Map<String, Object>> maps); void uptateTable3(@Param("lists")List<Map<String, Object>> maps); void uptateTable4(@Param("lists")List<Map<String, Object>> maps);
调用方法:
@GetMapping("hello3") public String hello3(){ List<Map<String, Object>> maps = new ArrayList<>(); for (int i = 1; i < 3; i++) { HashMap<String, Object> hashMap = new HashMap<>(); hashMap.put("id",i); hashMap.put("name","name"+i); maps.add(hashMap); } foundationDataDao.uptateTable(maps); foundationDataDao.uptateTable2(maps); return "hello ZhaoJun333"; } @GetMapping("hello4") public String hello4(){ List<Map<String, Object>> maps = new ArrayList<>(); for (int i = 1; i < 5; i++) { HashMap<String, Object> hashMap = new HashMap<>(); hashMap.put("id",i); hashMap.put("name","name"+i); hashMap.put("age",i*100); hashMap.put("my_no",i+""+i); maps.add(hashMap); } foundationDataDao.uptateTable5(maps); return "hello ZhaoJun333"; }
就是因为懒,就会直接写了
1.根据id去更新一个值, 根据id去更新name值
第一种写法
<update id="uptateTable" parameterType="java.util.Map"> update `student` <trim prefix="set" suffixOverrides=","> <foreach collection="lists" separator="" item="list" open="name = case id" close="end, "> when #{list.id} then #{list.name} </foreach> </trim> <where> <foreach collection="lists" separator="or" item="list"> id = #{list.id} </foreach> </where> </update>
第二种写法
<update id="uptateTable2" parameterType="java.util.Map"> update `student` <trim prefix="set" suffixOverrides=","> <trim prefix="name =case" suffix="end,"> <foreach collection="lists" item="cus"> <if test="cus.name!=null"> when id=#{cus.id} then #{cus.name} </if> </foreach> </trim> </trim> <where> <foreach collection="lists" separator="or" item="list"> id = #{list.id} </foreach> </where> </update>
2.根据id去更新name和age值
第一种写法
<update id="uptateTable3" parameterType="java.util.Map"> update `student` <trim prefix="set" suffixOverrides=","> <foreach collection="lists" separator="" item="list" open="name = case id" close="end, "> when #{list.id} then #{list.name} </foreach> <foreach collection="lists" separator="" item="list" open="age = case id" close="end, "> when #{list.id} then #{list.age} </foreach> </trim> <where> <foreach collection="lists" separator="or" item="list"> id = #{list.id} </foreach> </where> </update>
第二种写法
<update id="uptateTable4" parameterType="java.util.Map"> update `student` <trim prefix="set" suffixOverrides=","> <trim prefix="name =case" suffix="end,"> <foreach collection="lists" item="cus"> <if test="cus.name!=null"> when id=#{cus.id} then #{cus.name} </if> </foreach> </trim> <trim prefix="age =case" suffix="end,"> <foreach collection="lists" item="cus"> when id=#{cus.id} then #{cus.age} </foreach> </trim> </trim> <where> <foreach collection="lists" separator="or" item="list"> id = #{list.id} </foreach> </where> </update>
当然了大家也可以去使用mybatis的batch批量更新.
这种方式不用调节任何数据库的参数,是属于语法层面的.
就写这么多了,祝愿大家端午节快乐