批量插入,批量修改的sql

Posted xue123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批量插入,批量修改的sql相关的知识,希望对你有一定的参考价值。

sql 1  批量插入

<insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List" >
<selectKey resultType="long" keyProperty="id" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into user_contacts_info (id,user_id,old_id,cont_type,
cont_ship, cont_status, cont_source,
cont_user_name, cont_id_card, cont_mobile1,
cont_mobile2, cont_mobile3, cont_tell,
cont_addr, cont_addr_hk, cont_addr_com,
cont_tell_com, e_mail, remark, create_time,
cont_name_com,update_time)
values
<foreach collection="list" item="item" index="index" separator="," >
(#{item.id,jdbcType=INTEGER},#{item.userId,jdbcType=INTEGER},#{item.oldId,jdbcType=INTEGER}, #{item.contType,jdbcType=VARCHAR},
#{item.contShip,jdbcType=VARCHAR}, #{item.contStatus,jdbcType=VARCHAR}, #{item.contSource,jdbcType=VARCHAR},
#{item.contUserName,jdbcType=VARCHAR}, #{item.contIdCard,jdbcType=VARCHAR}, #{item.contMobile1,jdbcType=VARCHAR},
#{item.contMobile2,jdbcType=VARCHAR}, #{item.contMobile3,jdbcType=VARCHAR}, #{item.contTell,jdbcType=VARCHAR},
#{item.contAddr,jdbcType=VARCHAR}, #{item.contAddrHk,jdbcType=VARCHAR}, #{item.contAddrCom,jdbcType=VARCHAR},
#{item.contTellCom,jdbcType=VARCHAR},#{item.eMail,jdbcType=VARCHAR}, #{item.remark,jdbcType=VARCHAR},
#{item.createTime,jdbcType=TIMESTAMP}, #{item.contNameCom,jdbcType=VARCHAR},
#{item.updateTime,jdbcType=TIMESTAMP})
</foreach>
</insert>

sql 2 批量插入,表设置为主键自增长
<insert id="insertByBatch" parameterType="java.util.List">
insert into monitor_log
(monitor_id, monitor_date, monitor_stats,
monitor_info,
monitor_product_id, monitor_repair_date,
monitor_repair_userid)
values
<foreach collection="list" item="item" index="index"
separator=",">
(#{item.monitorId,jdbcType=INTEGER},
#{item.monitorDate,jdbcType=TIMESTAMP},
#{item.monitorStats,jdbcType=TINYINT},
#{item.monitorInfo,jdbcType=VARCHAR},
#{item.monitorProductId,jdbcType=INTEGER},
#{item.monitorRepairDate,jdbcType=TIMESTAMP},
#{item.monitorRepairUserid,jdbcType=INTEGER})
</foreach>
</insert>

sql 3 批量修改
 <update id="updateBatch" parameterType="java.util.List">
update monitor_log
<trim prefix="set" suffixOverrides=",">
<trim prefix="monitor_date =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.monitorDate !=null ">
when monitor_id=#{item.monitorId} then #{item.monitorDate,jdbcType=TIMESTAMP}
</if>
</foreach>
</trim>
<trim prefix="monitor_stats =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.monitorStats !=null ">
when monitor_id=#{item.monitorId} then #{item.monitorStats,jdbcType=TINYINT}
</if>
</foreach>
</trim>
<trim prefix="monitor_info =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.monitorInfo !=null ">
when monitor_id=#{item.monitorId} then #{item.monitorInfo,jdbcType=VARCHAR}
</if>
</foreach>
</trim>
<trim prefix="monitor_product_id =case" suffix="end,">
<foreach collection="list" item="item" index="index">
<if test="item.monitorProductId !=null ">
when monitor_id=#{item.monitorId} then #{item.monitorProductId,jdbcType=INTEGER}
</if>
</foreach>
</trim>
where monitor_id in
<foreach collection="list" index="index" item="item"
separator="," open="(" close=")">
#{item.monitorId,jdbcType=BIGINT}
</foreach>
</update>




















































































以上是关于批量插入,批量修改的sql的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL批量插入语句怎么写

SQL批量插入修改

在Oracle中,通过一个INSERT ALL语句批量插入数据

使用“一条”SQL语句完成批量插入和批量删除操作

使用“一条”SQL语句完成批量插入和批量删除操作

MYSQL批量插入数据库实现语句性能分析