java sql 测试批量插入效率

四种模式下的批量插入测试响应:
插入一万条数据,耗时情况ms:

复制代码
[{
    "taskName": "循环插入",
    "timeMillis": 20771,
    "timeSeconds": 20.771
}, {
    "taskName": "批量保存",
    "timeMillis": 1903,
    "timeSeconds": 1.903
}, {
    "taskName": "Mybatis自带批量保存",
    "timeMillis": 12737,
    "timeSeconds": 12.737
}, {
    "taskName": "spring jdbcTemplate",
    "timeMillis": 11582,
    "timeSeconds": 11.582
}]
复制代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * 默认情况,循环插入
 * 14.312
 */
public void testMybatisInsertSave(Integer num) {
    List<BatchInsertDemo> batchInsertDemoList = initDemos(num);
    batchInsertDemoList.forEach(batchInsertDemo -> {
        batchInsertDemoMapper.insert(batchInsertDemo);
    });
}
 
/**
 * 批量保存的情况
 * 1.177
 */
public void testMybatisInsertBatchSave(Integer num) {
    List<BatchInsertDemo> batchInsertDemoList = initDemos(num);
    batchInsertDemoMapper.insertBatch(batchInsertDemoList);
}
 
 
/**
 * Mybatis 自带批量保存
 * 8.087
 */
public void testMybatisInsertSqlSessionBatchSave(Integer num) {
    List<BatchInsertDemo> batchInsertDemoList = initDemos(num);
    SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
    BatchInsertDemoMapper batchInsertDemoMapper = sqlSession.getMapper(BatchInsertDemoMapper.class);
    batchInsertDemoList.forEach(batchInsertDemo -> {
        batchInsertDemoMapper.insert(batchInsertDemo);
    });
    sqlSession.commit();
}
 
/**
 * spring jdbcTemplate
 * 7.132
 */
public void testJdbcInsertBatchSave(Integer num) {
    List<Object[]> demoList = initJDBCDemos(num);
    String sql = "INSERT INTO  `demo`(  `id`, `name`,\n" +
            "\t\t`key_word`,\n" +
            "\t\t`punch_time`,\n" +
            "\t\t `salary_money`,\n" +
            "\t\t `bonus_money`,\n" +
            "\t\t `sex`, `age`, `birthday`,\n" +
            "\t\t  `email`, `content`)\n" +
            "\t\tVALUES (?,?,?,?,?,?,?,?,?,?,?)";
 
    jdbcTemplate.batchUpdate(sql, demoList);
}

  

https://gitee.com/caoyeoo0/xc-springboot/blob/mybatis/批量插入/src/main/java/com/xc/xcspringboot/service/impl/BatchInsertDemoServiceImpl.java

posted @   草木物语  阅读(215)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-12-07 es 布尔查询
2021-12-07 js 打印 去除页眉页脚
2017-12-07 bootstrap 栅栏系统
2016-12-07 input placeholder属性 样式修改(颜色,大小,位置)
2016-12-07 div+css:div中图片垂直居中
2016-12-07 div+css:两个div并排等高 (table-cell)
点击右上角即可分享
微信分享提示