JAVA使用HikariCP开启执行批量执行插入数据

星期五, 2021-04-30 | Author: Lee | JAVA-and-J2EE | 1,120 views

1.使用HikariConfig配置如下: 开启批量处理

#mysql config
jdbcUrl=jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai
username=root
password=root
driverClassName=com.mysql.cj.jdbc.Driver
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
dataSource.useServerPrepStmts=true
dataSource.useLocalSessionState=true
dataSource.rewriteBatchedStatements=true
dataSource.cacheResultSetMetadata=true
dataSource.statementsPerBatch=5000
#dataSource.cacheServerConfiguration=true
#dataSource.elideSetAutoCommits=true
dataSource.maintainTimeStats=false
dataSource.useCursorFetch=true
autoCommit=true
maximumPoolSize=10

2.使用批量执行语句

 
String sql ="INSERT INTO MyTable(ColA, ColB, ColC) VALUES (?, ?, ?)";  //ok batch
sql ="INSERT INTO MyTable(ColA, ColB, ColC) VALUE (?, ?, ?)";  //no batch only Inserting one record at a time
queryRunner.batch(,
        new Object[][] {
            {"A1", "B1", "C1"},
            {"A2", "B2", "C2"},
            {"A3", "B3", "C3"}});

小结:因为使用了 VALUE 不是 VALUES而无法批量生效,一直跟踪debug才发现,浪费了半天时间囧.

文章作者: Lee

本文地址: https://www.pomelolee.com/2148.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

相关文章

文章分类

Links

Meta