欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL select、insert、update批量操作语句代码实例

程序员文章站 2022-05-15 23:45:20
项目中经常的需要使用批量操作语句对数据进行操作,使用批量语句的时候需要注意mysql数据库的条件限制,这里主要是数据的大小限制,这里对批量插入、批量查找及批量更新的基础操作进行下简单的汇总。1、批量i...

  项目中经常的需要使用批量操作语句对数据进行操作,使用批量语句的时候需要注意mysql数据库的条件限制,这里主要是数据的大小限制,这里对批量插入、批量查找及批量更新的基础操作进行下简单的汇总。

1、批量insert插入语句,如下的语句在测试环境尝试过1w+的批量插入,具体数量跟表结构及字段内容有关系。

<insert id=”addtextpushlist”paramertertype = “java.util.list”>
  insert into sys_text_push(
      push_id,
      user_um_account,
      user_name,
      section,
      user_mobile,
      push_status,
      promote_id,
      created_by,
      creation_date,
      enabled_flag  
)values
<foreach  collection=”list”  item = “item” separator=”,”>
  (
    #{item.pushid},
    #{item,userumaccount},
    #{item.username},
    #{item.section},
    #{item.usermobile},
    #{item.pushstatus},
    #{item.promoteid},
    #{item.createdby},
    #{item.creationdate},
    #{item.enabledflag}
)
</foreach>
</insert>

2、批量select查询语句

<select  id = “gettextpromotebyids”  parametertype = “java.util.list” resultmap = “textpromotemap”>
  select 
    *
  from  sys_text_promote 
  where
     text_push_flag = 1
    and  promote_id  in
  <foreach  collection=”list”  item = “item” open=”(” separator=”,” close =”)”>
  #{item}
</foreach>
</select>

3、批量update语句。

第一种情况是需更新的值相同:

<update  id = “updatepushstatus” parametertype = “java.util.list”>
  update
     sys_text_push
  set
    push_status = 1,
    last_update_date = now()
  where
      push_id  in
<foreach  collection=”list”  item = “item” open=”(” separator=”,” close =”)”>
  #{item}
</foreach>
</update>

第二种情况是需更新的值不同:

<update  id = “updatepushstatus” parametertype = “java.util.list”>
<foreach  collection=”list”  item = “item” index = “index”>
  update
     sys_text_push
  set
    push_status = #{item.pushstatus},
    last_update_date = now()
  where
      push_id  = #{item.pushid}
</foreach>
</update>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。