mybatis中如何实现批量插入和批量更新呢?
下文笔者讲述mybatis中批量插入和批量更新的操作方法分享,如下所示
批量插入和批量更新的实现思路
使用mybatis中的foreach标签
即可拼接一个批量插入和批量更新的sql脚本
这就是mybatis批量插入和批量更新的实现思路
例
批量插入
<insert id="insertBatch" >
insert into table ( <include refid="Base_Column_list" /> )
values
<foreach collection="list" item="i" index="index" separator=",">
(#{i.id},#{i.userId},#{i.name})
</foreach>
</insert>
批量更新
使用case when语句
变相实现批量更新
<update id="updateBatchById" parameterType="list">
update table
<trim prefix="set" suffixOverrides=",">
<trim prefix="user_mobile =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.userMobile!=null">
when id=#{i.Id} then #{i.userMobile}
</if>
</foreach>
</trim>
<trim prefix="name =case" suffix="end,">
<foreach collection="list" item="i" index="index">
<if test="i.name!=null">
when id=#{i.Id} then #{i.name}
</if>
</foreach>
</trim>
</trim>
where
<foreach collection="list" separator="or" item="i" index="index" >
id=#{i.Id}
</foreach>
</update>
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


