jdbc.properties文件
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&rewriteBatchedStatements=true
jdbc.driverName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123
其中,数据库连接中的参数rewriteBatchedStatements
相当重要,如果不开启rewriteBatchedStatements=true
,那么jdbc会把批量插入当做一行行的单条处理,也即没有达到批量插入的效果。
@Resource(name="dataSource")
private DataSource dataSource;
/* JDBC 的批量操作 */
@Test
public void insertBatch() {
Connection connection=dataSource.getConnection();
connection.setAutoCommit(false);
String sql="insert into notify_record(aa,bb, cc, dd, ee, ff, gg, hh, ii) " +
" values(?,?,?,?,?,?,?,?,?) ";
PreparedStatement statement=connection.prepareStatement(sql);
for (int i = 0; i < 1000 ; i++) {
statement.setString(1, "aaa"+i);
statement.setString(2, "bbb"+i);
statement.setInt(3, i);
statement.setInt(4, i);
statement.setString(5, ""+i);
statement.setString(6, "ddd"+i);
statement.setString(7, "eee"+i);
statement.setString(8, "fff"+i);
statement.setString(9, "ggg"+i);
statement.addBatch();
}
long start=System.currentTimeMillis();
statement.executeBatch();
connection.commit();
connection.close();
statement.close();
System.out.print("耗时:");
System.out.println(System.currentTimeMillis()-start);
}
记录数 | 耗时(ms) |
---|---|
100 | 210 |
1000 | 551 |
10000 | 1963 |
100000 | 10280 |
ExecutorType=BATCH
)java实现:
Map<String,Object> param = Maps.newHashMap();
param.put("recordList", recordList);
recordDao.insertList(param);
mapper.xml如下:
<insert id="insertList" parameterType="java.util.Map">
insert into notify_record
(aa,bb,cc,dd,ee,ff,gg,hh,ii)
values
<foreach collection="recordList" item="recordList" open="" close=""
separator=",">
(
null,
#{recordList.aa,jdbcType=VARCHAR},
#{recordList.bb,jdbcType=VARCHAR},
#{recordList.cc,jdbcType=VARCHAR},
#{recordList.dd,jdbcType=VARCHAR},
#{recordList.ee,jdbcType=VARCHAR},
#{recordList.ff,jdbcType=VARCHAR},
#{recordList.gg,jdbcType=VARCHAR},
#{recordList.hh,jdbcType=VARCHAR},
#{recordList.ii,jdbcType=VARCHAR},
)
foreach>
;
insert>
记录数 | 耗时(ms) |
---|---|
100 | 414 |
1000 | 1035 |
10000 | 4899 |
100000 | 62752 |
记录数 | 耗时(ms) |
---|---|
100 | 9624 |
1000 | 102275 |
10000 | 失去耐心 |
100000 | 失去耐心 |