Java 使用jdbcTemplate.batchUpdate进行批插入
Java 使用jdbcTemplate.batchUpdate进行批插入,java,mysql,jdbc,spring-jdbc,Java,Mysql,Jdbc,Spring Jdbc,jdbcTemplate.batchUpdate是否在数据库服务器上执行多个单插入语句或1个多值列表插入 我知道它会立即将完整的查询负载发送到服务器,但不确定执行是如何进行的 有人能解释/帮助吗?问题: jdbcTemplate.batchUpdate是否在数据库服务器上执行多个单插入语句或1个多值列表插入 发件人: 我对int[]org.springframework.jdbc.core.JdbcTemplate.batchUpdate(String sql,List batchArgs,in
jdbcTemplate.batchUpdate是否在数据库服务器上执行多个单插入语句或1个多值列表插入
我知道它会立即将完整的查询负载发送到服务器,但不确定执行是如何进行的
有人能解释/帮助吗?问题:
jdbcTemplate.batchUpdate是否在数据库服务器上执行多个单插入语句或1个多值列表插入
发件人:
我对int[]org.springframework.jdbc.core.JdbcTemplate.batchUpdate(String sql,List batchArgs,int[]argTypes)很好奇。
TL;DR:它执行1个多值列表。
Spring框架是开源的,因此很容易查看源代码并了解它的实际功能
可以看出,它创建一个PreparedStatement
,进入一个调用addBatch()
的循环,最后调用executeBatch()
因此,简单的答案是:1多值列表
完整的答案是,它可能会向数据库服务器发送一条SQL语句和一个多值列表,但实际上如何实现批处理完全取决于JDBC驱动程序,主要受通信协议支持的内容限制,因此,唯一确定的方法是跟踪与服务器的通信。中有5种不同的batchUpdate
方法。他们可能有5种不同的方式,那么你问的是哪一种呢?我很好奇int[]org.springframework.jdbc.core.JdbcTemplate.batchUpdate(String sql,List batchArgs,int[]argTypes)
你已经知道它会发送整个负载了吗?你这是什么意思。批处理意味着,如果DB支持,那么最终将有一条INSERT语句包含多个值@戴纳姆先生不正确。批处理意味着将多个请求一起发送到数据库。通常,这意味着一条语句,该语句的许多参数集,但您也可以批处理多条语句,否则它的用途和调用的基础是什么?即JDBC端,而不是服务器端。JDBC驱动程序如何实现这一点,并不一定是DB服务器如何执行它。例如,对于MySQL,驱动程序需要将单个插入到。。。值()
语句,驱动程序将这些语句重写为1个插入到。。。VALUES(),VALUES()
statement。经过一些测试,我发现根据连接字符串中的rewriteBatchedStatements=true属性,结果会有所不同。对于相同的代码,在sql常规日志中使用rewriteBatchedStatements=false设置autocommit=0插入到。。。。插入。。。。插入。。。。commit with rewriteBatchedStatements=true SET autocommit=0插入到…值(),(),()提交中
@Override
public int[] batchUpdate(String sql, List<Object[]> batchArgs, final int[] argTypes) throws DataAccessException {
if (batchArgs.isEmpty()) {
return new int[0];
}
return batchUpdate(
sql,
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Object[] values = batchArgs.get(i);
int colIndex = 0;
for (Object value : values) {
colIndex++;
if (value instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) value;
StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
}
else {
int colType;
if (argTypes.length < colIndex) {
colType = SqlTypeValue.TYPE_UNKNOWN;
}
else {
colType = argTypes[colIndex - 1];
}
StatementCreatorUtils.setParameterValue(ps, colIndex, colType, value);
}
}
}
@Override
public int getBatchSize() {
return batchArgs.size();
}
});
}
@Override
public int[] batchUpdate(String sql, final BatchPreparedStatementSetter pss) throws DataAccessException {
if (logger.isDebugEnabled()) {
logger.debug("Executing SQL batch update [" + sql + "]");
}
int[] result = execute(sql, (PreparedStatementCallback<int[]>) ps -> {
try {
int batchSize = pss.getBatchSize();
InterruptibleBatchPreparedStatementSetter ipss =
(pss instanceof InterruptibleBatchPreparedStatementSetter ?
(InterruptibleBatchPreparedStatementSetter) pss : null);
if (JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
for (int i = 0; i < batchSize; i++) {
pss.setValues(ps, i);
if (ipss != null && ipss.isBatchExhausted(i)) {
break;
}
ps.addBatch();
}
return ps.executeBatch();
}
else {
List<Integer> rowsAffected = new ArrayList<>();
for (int i = 0; i < batchSize; i++) {
pss.setValues(ps, i);
if (ipss != null && ipss.isBatchExhausted(i)) {
break;
}
rowsAffected.add(ps.executeUpdate());
}
int[] rowsAffectedArray = new int[rowsAffected.size()];
for (int i = 0; i < rowsAffectedArray.length; i++) {
rowsAffectedArray[i] = rowsAffected.get(i);
}
return rowsAffectedArray;
}
}
finally {
if (pss instanceof ParameterDisposer) {
((ParameterDisposer) pss).cleanupParameters();
}
}
});
Assert.state(result != null, "No result array");
return result;
}