首先推薦使用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(); } }
或類似於
- PreparedStatement ps = conn.prepareStatement(
- "INSERT into employees values (?, ?, ?)");
- for (n = 0; n < 100; n++) {
- ps.setString(name[n]);
- ps.setLong(id[n]);
- ps.setInt(salary[n]);
- ps.addBatch();
- }
刪除的,類似。
其次,使用下面的方式:
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);