mybaits批量修改,批量添加,批量删除

主要用到mybaits的foreach标签

foreach标签的属性主要有
collection :被遍历的参数,如果是list ,collection 的属性值就是list;如果是array数组,collection的属性值为array;如果被遍历的参数在map中,collection 的属性值就是key值
item:集合中每个元素的别名(随便命名)
index:每次遍历时的下标(随便取名)
open:表示该语句前面拼接点啥东西比如(,value(,...
separator:表示在每次进行迭代之间以什么符号作为分隔 符,
close:表示该语句后面拼接点啥东西比如 ")"...

test1表: 

批量修改:

xml:

<update id="update" parameterType="com.xjz.user.bean.User">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE test2
            SET age = #{item.age},
            SET name = #{item.name}
            WHERE id = #{item.id,jdbcType=INTEGER}
        </foreach>
</update>    

mapper:

 void update(List<User> list);

批量添加:

xml:

 <insert id="insert" parameterType="com.xjz.user.bean.User">
        INSERT INTO test2
        (age,name)
        VALUES
        <foreach collection="list" item="item" index="index"  separator=",">
          (#{item.age},#{item.name})
        </foreach>
    </insert>

mapper:

void insert(List<User> list);

  

批量删除:

  <delete id="delete" parameterType="java.util.List">
        <foreach collection="list" item="id" separator=";" index="index">
            DELETE FROM test1
            WHERE id = #{id}
        </foreach>
    </delete>

mapper:

void insert(List list);

  

原文地址:https://www.cnblogs.com/bigbigxiao/p/13602505.html