Mybatis批量添加、修改和删除操作记录

1.批量添加

<insert id="batchSave" parameterType="java.util.List">
	INSERT INTO test(
	    id,
	    ...
	) VALUES  
	<foreach collection="list" item="item" index="index" separator=",">  
	    (#{item.id},
	    #{item....},)
	foreach>
insert>

2.批量修改


<update id="batchUpdate" parameterType="java.util.List">
    <foreach collection="list" item="item" separator=";">
        update
        test set
        <trim prefix="" suffix=" " suffixOverrides=",">
		<if test="item.val != null and item.val != ''">
                VAL= #{item.val,jdbcType=VARCHAR},
		if>
        trim>
        where ID = #{item.id,jdbcType=VARCHAR}
	foreach>
update>

3.批量逻辑删除

<update id="batchDelete" parameterType="java.util.List">
	UPDATE test SET 
	    del_flag = "1"
	WHERE id IN 
	<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">  
        #{item.id} 
	foreach>
 update>

4.批量物理删除

<delete id="deleteByIds" parameterType="String">
    delete from test where id in 
    <foreach item="id" collection="array" open="(" separator="," close=")">
        #{id}
    foreach>
delete>

你可能感兴趣的:(MyBatis,mybatis,mysql)