首先推荐使用PreparedStatement的批量处理操作。

   Connection conn = null;
PreparedStatement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL,USER,PASS); String SQL = "INSERT INTO Employees(id,first,last,age) " +
"VALUES(?, ?, ?, ?)"; stmt = conn.prepareStatement(SQL); conn.setAutoCommit(false);
    //数据量多的 可以使用for循环批量
stmt.setInt( 1, 400 );
stmt.setString( 2, "Python" );
stmt.setString( 3, "Zhang" );
stmt.setInt( 4, 33 );
stmt.addBatch(); stmt.setInt( 1, 401 );
stmt.setString( 2, "C++" );
stmt.setString( 3, "Huang" );
stmt.setInt( 4, 31 );
stmt.addBatch();
int[] count = stmt.executeBatch();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
或类似于
  1. PreparedStatement ps = conn.prepareStatement(
  2. "INSERT into employees values (?, ?, ?)");
  3. for (n = 0; n < 100; n++) {
  4. ps.setString(name[n]);
  5. ps.setLong(id[n]);
  6. ps.setInt(salary[n]);
  7. ps.addBatch();
  8. }

删除的,类似。

其次,使用下面的方式:

1.批量新增

int count = list.size() / 500; //自己定义一次批量处理条数(不要太高,我这边遇到批量超过20000条,速度明显变慢,批量15w等的急死,如果太大的话,还有可能超过mysql默认的最大长度1M的限制)
int remainder = list.size() % 500;
List<Map<String, String>> oneSubmit = null;
for(int i = 0; i<= count; i++) {
  if(i == count) {
    if(remainder > 0) {
    oneSubmit = list.subList(500*i, 500*i+remainder);
    ceShiDao.batchInsertZztContactTmp(oneSubmit);
    }
  }else {
    oneSubmit = list.subList(500*i, 500*(i+1));
    ceShiDao.batchInsert(oneSubmit);
  }
} @Insert({"<script>",
"insert into xxx (file_name, file_id, user_id, phone,create_user_id,update_user_id,status,params) values ",
"<foreach collection='list' item='item' separator=','>",
" (#{item.fileName},",
" #{item.fileId},",
" #{item.userId},",
" #{item.phone},",
" #{item.createUserId},",
" #{item.updateUserId},",
" #{item.status},",
" #{item.params})",
"</foreach>",
"</script>"})
int batchInsert(@Param("list") List<Map<String, String>> list);

2.批量删除

int count = contactIdList.size() / 500;
int remainder = contactIdList.size() % 500;
List<Integer> oneSubmit = null;
for(int i = 0; i<= count; i++) {
  if(i == count) {
    if(remainder > 0) {
      oneSubmit = contactIdList.subList(500*i, 500*i+remainder);
      ceShiDao.batchDelete(oneSubmit);
    }
  }else {
    oneSubmit = contactIdList.subList(500*i, 500*(i+1));
    ceShiDao.batchDelete(oneSubmit);
  }
} @Delete({"<script>",
"delete from xxx",
"where tmp_contact_id in ",
" <foreach collection='list' item='item' open='(' separator=',' close=')'>",
" #{item}",
" </foreach>",
"</script>"
})
int batchDelete(@Param("list") List<Integer>list);

mysql批量新增和批量删除的更多相关文章

  1. mybatis 注解形式设置批量新增、批量更新数据

    1. 批量更新: @Update({"<script>" + "<foreach collection=\"smsConfigTemplate ...

  2. 基于JQuery easyui,gson的批量新增/修改和删除-servlet版

    最近项目需要用到在页面进行批量操作,做了一些这方面的学习,参照网上的资料写了个小例子,记录一下: 准备 引入gson-2.6.2.jar,这里使用gson而不使用json-lib,原因是json-li ...

  3. MyBatis基础入门《十三》批量新增数据

    MyBatis基础入门<十三>批量新增数据 批量新增数据方式1:(数据小于一万) xml文件 接口: 测试方法: 测试结果: =============================== ...

  4. mybatis,批量新增、修改,删除

    转载自:http://blog.csdn.net/sanyuesan0000/article/details/19998727 最近需要用到Mybatis批量新增oracle数据库,刚开始在网上找到的 ...

  5. mybatis学习之路----mysql批量新增数据

    原文:https://blog.csdn.net/xu1916659422/article/details/77971867 接下来两节要探讨的是批量插入和批量更新,因为这两种操作在企业中也经常用到. ...

  6. MYSQL-JDBC批量新增-更新-删除

    目录 1 概述 2 开启MYSQL服务端日志 3 深入MYSQL/JDBC批量插入 3.1 从一个例子出发 3.2 JDBC的批量插入操作 3.3 两个常被忽略的问题 3.5 误区 4 MYSQL/J ...

  7. MySQL根据表前缀批量修改、删除表

    注意:请先调试好,以及做好备份,再执行操作. 批量修改表 批量给前缀为 xushanxiang_content_ 的表增加一个 username 的字段: SELECT CONCAT('ALTER T ...

  8. mybatis 批量新增-批量修改-批量删除操作

    mapper.xml <!-- 批量新增 --> <insert id="saveBatch" parameterType="java.util.Lis ...

  9. EntityFramework进阶(四)- 实现批量新增

    本系列原创博客代码已在EntityFramework6.0.0测试通过,转载请标明出处 我们可以结合Ado.Net的SqlBulkCopy实现SqlServer数据库的批量新增,其他类型的数据库的批量 ...

随机推荐

  1. Colossal Fibonacci Numbers! UVA - 11582(快速幂,求解)

    Problem Description The i’th Fibonacci number f(i) is recursively defined in the following way: •f(0 ...

  2. jupyter lab 安装

    在windows下安装jupyter 特别简单 首先你需要有Anaconda or python的环境变量,这里我就不说怎么安装环境变量了,网上一大堆教程 启动黑窗口,下载jupyter pip in ...

  3. 植物大战僵尸:寻找召唤僵尸关键CALL

    实验目标:通过遍历寻找召唤僵尸的CALL,通过调用CALL出现自定义的僵尸,加速僵尸的出现. 僵尸CALL的遍历技巧: 我们可以通过僵尸出现在屏幕中的个数来遍历寻找僵尸出现的CALL 首先打开CE-& ...

  4. 【原创】大叔经验分享(76)confluence和jira配置

    一 下载 confluence https://product-downloads.atlassian.com/software/confluence/downloads/atlassian-conf ...

  5. 编写Dockerfile自定义镜像

    要求 编写一个Dockerfile自定义centos镜像,要求在容器内部可以使用vim和ifconfig命令,并且登入落脚点为/usr/local 编写Dockerfile FROM centos M ...

  6. 简单SQL注入试探、一

    DVWA——简单SQL注入小记 前不久刚开始接触SQL注入,今天来记录一些最近的一些收获和一些SQL注入方面的知识. 主要是基于DVWA这个开源的平台来进行练习. 废话不多说开始解题. 从简单的SQL ...

  7. bash shell脚本之使用expr运算

    bash shell中的数学运算 cat test7: #!/bin/bash # An example of using the expr command var1= var2= var3=`exp ...

  8. 测试clang-format的格式化效果

    我自己写的业余框架已告一段落,主体功能已完成,剩下的就是优化.第一个要优化的,就是代码格式.我一直是用编辑器写代码的,从之前的UltraEdit到notepad++到sublime text,再到现在 ...

  9. rabbitmq 使用PhpAmqpLib

    rabbitmq类 rabbitmq.php <?php require_once __DIR__ . '/vendor/autoload.php'; use PhpAmqpLib\Connec ...

  10. 利用FastReport直接生成条码

    procedure TForm1.Button1Click(Sender: TObject); var x:TfrxbarCodeView; begin x:=TfrxbarCodeView.Crea ...