Bootstrap

springboot执行批量插入_多线程多批量插入大数据

参考  https://blog.csdn.net/xunwei0303/article/details/80241340?utm_source=blogxgwz1

创建多个线程,每个线程处理一批数据。

1. 创建表(mysql)

CREATE TABLE TEST_BATCH_INSERT

(

TEST_ID bigint PRIMARY key,

TEST_NAME VARCHAR(100),

AGE INT(5),

CREATE_TIME DATETIME DEFAULT current_timestamp,

UPDATE_TIME DATETIME DEFAULT current_timestamp

) comment '测试批量插入';

2. java bean

public class TestBatchInsertInfo {

private Long testId;

private String testName;

private Integer age;

private Date createTime;

private Date updateTime;

// 省略getter/setter

}

3. dao

public interface ITestBatchInsertMapper {

void batchInsert(List list);

}

4. mapper.xml

INSERT INTO TEST_BATCH_INSERT

(

TEST_ID, TEST_NAME, AGE, CREATE_TIME, UPDATE_TIME

)

VALUES

(

#{log.testId, jdbcType=NUMERIC}, #{log.testName, jdbcType=VARCHAR}, #{log.age, jdbcType=NUMERIC},

sysdate(), sysdate()

)

5. 多线程

public class TestBatchInsertThread implements Runnable {

private ITestBatchInsertMapper testBatchInsertMapper;

/** 数据集合 */

private List list;

/** 每个线程处理的起始数据 */

private CountDownLatch begin;

/** 每个线程处理的结束数据 */

private CountDownLatch end;

public TestBatchInsertThread() {

}

public TestBatchInsertThread(List list, CountDownLatch begin, CountDownLatch

;