一.配置
- 批量操作jdbc url需添加allowMultiQueries=true属性:
实例:spring.datasource.url=jdbc:mysql:[url]?useUnicode=true&characterEncoding=UTF8&autoReconnect=true&allowMultiQueries=true
二.批量增
mybatis
<insert id="" parameterType="java.util.List">
insert into hello_words (id, value)
values
<foreach collection="list" item="item" index="index" separator="," open="(" close=")" >
#{item.wordNo},#{item.value}
</foreach>
</insert>
mysql执行语句
insert into hello_words (id, value) insert into hello_words (id, value) values (1,2),{2,3)
三.批量更新
mybatis单属性更新
<update id="" parameterType="java.util.List">
UPDATE hello_words
SET value=#{value}
WHERE id IN
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id}
</foreach>
</update>
mysql单属性更新
UPDATE hello_words SET value=#{value} WHERE id IN(1,2)
mybatis多属性更新
<update id="" parameterType="java.util.List">
update hello_words
<trim prefix="set" suffixOverrides=",">
value1 =
<foreach collection="list" index="index" item="item" open="case ID" close="end" separator="" >
when #{item.id} then #{item.value}
</foreach>
,
value2=
<foreach collection="list" index="index" item="item" open="case ID" close="end" separator="" >
when #{item.id} then #{item.value2}
</foreach>
</trim>
WHERE id IN
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id}
</foreach>
</update>
mysql多属性更新
update hello_words
SET value=
case when id =1 then 2 end
case when id =2 then 2 end
where id in(1,2)
四.批量删除
<delete id="">
delete from hello_words
where id in
<foreach item="item" collection="list" open="(" separator="," close=")">
#{item.id}
</foreach>
</delete>