批量更新操作
批量更新逻辑:
底层真正的批量更新(一次请求发送批量更新语句)
底层循环发送语句(假批量更新)—本质上就是一条一条更新
批量更新逻辑判断
JdbcTemplate
JdbcUtils.supportsBatchUpdates(ps.getConnection())—判断连接是否支持批量更新
publicint[] batchUpdate(Stringsql,finalBatchPreparedStatementSetterpss)throwsDataAccessException {
if(logger.isDebugEnabled()) {
logger.debug("Executing SQL batch update ["+sql+"]");
}
returnexecute(sql,newPreparedStatementCallback() {
publicint[] doInPreparedStatement(PreparedStatementps)throwsSQLException {
try{
intbatchSize=pss.getBatchSize();
InterruptibleBatchPreparedStatementSetteripss=
(pssinstanceofInterruptibleBatchPreparedStatementSetter ?
(InterruptibleBatchPreparedStatementSetter)pss:null);
if(JdbcUtils.supportsBatchUpdates(ps.getConnection())) {
for(inti= 0;i
pss.setValues(ps,i);
if(ipss!=null&&ipss.isBatchExhausted(i)) {
break;
}
ps.addBatch();
}
returnps.executeBatch();
}
else{
ListrowsAffected=newArrayList();
for(inti= 0;i
pss.setValues(ps,i);
if(ipss!=null&&ipss.isBatchExhausted(i)) {
break;
}
rowsAffected.add(ps.executeUpdate());
}
int[]rowsAffectedArray=newint[rowsAffected.size()];
for(inti= 0;i
rowsAffectedArray[i] =rowsAffected.get(i);
}
returnrowsA