Mybatisd对MySQL批量插入、批量更新及批量删除语句

本文介绍了使用MyBatis进行批量插入、更新和删除的具体实现方法,包括XML映射文件中的SQL语句及foreach标签的详细配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、批量插入

<span style="color:#333333"><span style="color:#f8f8f2"><code class="language-none"><insert 

        id="insertBatch" 

        parameterType="java.util.List">

        insert into 

            t_student(name, age, class)

        values

        <foreach 

            collection="list" 

            item="item" 

            index="index" 

            separator=",">

            (

                #{item.name,jdbcType=VARCHAR}, 

                #{item.age,jdbcType=INTEGER},

                #{item.class,jdbcType=LONGVARCHAR}

            )

        </foreach>

   </insert></code></span></span>

2、批量更新

方式一:

<span style="color:#333333"><span style="color:#f8f8f2"><code class="language-none"><update id="updateBatch">

       <foreach 

            collection="list" 

            separator=";" 

            item="stud">

            update t_studetn set

                 name = #{stud.name},

                age = #{stud.age},

                class = #{stud.sex},

            where id = #{stud.id}

       </foreach>

</update></code></span></span>

方式二:

<span style="color:#333333"><span style="color:#f8f8f2"><code class="language-none"><update>

  id="updateBatch" 

  parameterType="list">

UPDATE t_student

    SET name = CASE id 

 <foreach 

collection="list" 

item="i" 

index="index">

        WHEN #{i.id} THEN #{i.name}

 </foreach>

    END, 

    age = CASE id 

     <foreach 

collection="list" 

item="i" 

index="index">

        WHEN #{i.id} THEN #{i.age}

 </foreach>

    END

WHERE id IN 

<foreach 

collection="list" 

separator="or" 

item="i" 

index="index" >

          id=#{i.id}

    </foreach>

</update></code></span></span>

3、批量删除

<span style="color:#333333"><span style="color:#f8f8f2"><code class="language-none"><delete id="deleteBatchByParams">

        delete from 

            t_student

        where 

            id IN

        <foreach 

            collection="ids" 

            item="item" 

            index="index" 

            open="(" close=")" 

            separator=",">

                #{item}

        </foreach>

    </delete></code></span></span>

 

item循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。
具体说明:在list和数组中是其中的对象,在map中是value。
该参数为必选。
collection

要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象没有默认的键。
当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:
如果User有属性List ids。入参是User对象,那么这个collection = "ids"
如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"
上面只是举例,具体collection等于什么,就看你想对那个元素做循环。
该参数为必选。

separator元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
openforeach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。
closeforeach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。
index在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值