Java mybatis 插入10w 条数据
- Java + mybatis + MySQL
- 分批次,list 循环插入
java service 代码
public void batchInsert() {
int size = 2000;
int i = 0;
List<Demo> list = Lists.newArrayList();
if (!CollectionUtils.isEmpty(list)) {
while (list.size() > size) {
List<Demo> subList = list.subList(0,size);
list = list.subList(size, list.size());
batchInsert2(subList);
i++;
}
if (!CollectionUtils.isEmpty(list)) {
batchInsert2(list);
}
}
}
private void batchInsert2(List<Demo> list) {
demoMapper.batchInsert(list);
}
mybatis xml文件
<insert id="batchInsert" >
INSERT INTO company_balance
(id, name)
VALUES
<foreach collection ="list" item="item" separator =",">
(#{item.id}, #{item.name})
</foreach >
</insert>