Mybatis 批量更新传入一个List 有更新!

2019-04-01

使用批处理来更新

SQL批处理语句是这样的


UPDATE tb_specification_option
SET option_name = CASE id
 WHEN 1 THEN 'optionName' 
 WHEN 2 THEN 'optionName' 
 WHEN 3 THEN 'optionName'
 END,
orders = CASE id
 WHEN 1 THEN 'orders' 
 WHEN 2 THEN 'orders' 
 WHEN 3 THEN 'orders'
 END
WHERE id IN (1,2,3)


写成mybatis的语句


<update id="updateBatch" parameterType="java.util.List">
  update tb_specification_option
  <trim prefix="set" suffixOverrides=",">
	<trim prefix="option_name =case" suffix="end,">
	 <foreach collection="list" item="item" index="index">
	  when id=#{item.id} then #{item.optionName}
    <foreach>
   <trim>
   <trim prefix="orders =case" suffix="end,">
	 <foreach collection="list" item="item" index="index">
	  when id=#{item.id} then #{item.orders}
	 <foreach>
   <trim>
 <trim>
  where id in
    <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
	#{item.id,jdbcType=BIGINT}
    <foreach>
<update>

然后就可以直接发一条语句更新了

评论
发表评论
validate
取消