美文网首页
MySQL 批量操作

MySQL 批量操作

作者: ce5154e79490 | 来源:发表于2019-06-09 15:44 被阅读0次

1.批量添加

sql

insert into t_student (id, name, age)
values (?,?,?),(?,?,?)

xml

<insert id="batchInsert" parameterType="java.util.List">
        insert into t_student
        (
        <include refid="Base_Column_List"/>
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.id},
            #{item.name},
            #{item.age}
            )
        </foreach>
    </insert>

mapper

int batchInsert(List<Student> list);

2.批量更新

xml

<update id="batchUpdate" parameterType="java.util.List">
    update t_student
    <trim prefix="set" suffixOverrides=",">
        <trim prefix="name =case" suffix="end,">
            <foreach collection="list" item="item" index="index">
                <if test="item.name!=null and item.name != ''">
                    when id=#{item.id} then #{item.name}
                </if>
            </foreach>
        </trim>
        <trim prefix=" age =case" suffix="end,">
            <foreach collection="list" item="item" index="index">
                <if test="item.age != null and item.age > 0">
                    when id=#{item.id} then #{item.age}
                </if>
            </foreach>
        </trim>
    </trim>
    where id in 
    <foreach collection="list" separator="," item="item" index="index"  open="(" close=")">
        #{item.id}
    </foreach>
</update>

mapper

int batchUpdate(List<Student> list);

3.批量删除

xml

<delete id="batchDelete" parameterType="java.lang.String">
        delete from t_student
        where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

mapper

int batchDelete(String[] array);

相关文章

网友评论

      本文标题:MySQL 批量操作

      本文链接:https://www.haomeiwen.com/subject/trfnxctx.html