Mysql-mybatis批量插入or更新

  • 插入
    <insert id="batchSave" >
    		insert into table_name (`name`,age)	values
    		<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
    			#{item.name, jdbcType=VARCHAR},
    			#{item.age, jdbcType=INTEGER}
    		</foreach>
    	</insert>
  • 更新
    <update id="updateNameBatch">
    		update user
    		<trim prefix="set" suffixOverrides=",">
    			<trim prefix="name =case" suffix="end,">
    				<foreach collection="list" item="i" index="index">
    					when id=#{i.id} then #{i.name}
    				</foreach>
    			</trim>
    			gmt_modify = now()
    		</trim>
    		where
    		<foreach collection="list" item="i" index="index" separator="or">
    			(id=#{i.id})
    		</foreach>
    	</update>
  •  

 

    原文作者:红黑树
    原文地址: https://my.oschina.net/xpx/blog/2208347
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞