gpt4 book ai didi

sql - hibernatetemplate 的批量更新有什么意义?

转载 作者:行者123 更新时间:2023-12-05 08:01:53 29 4
gpt4 key购买 nike

hibernatetemplate 的 bulkUpdate 实际上是在执行 bulkUpdate 吗?我查看了代码,它似乎没有执行 bulkUpdate。还是我遗漏了什么?

public int bulkUpdate(final String queryString, final Object... values) throws DataAccessException {
return executeWithNativeSession(new HibernateCallback<Integer>() {
public Integer doInHibernate(Session session) throws HibernateException {
Query queryObject = session.createQuery(queryString);
prepareQuery(queryObject);
if (values != null) {
for (int i = 0; i < values.length; i++) {
queryObject.setParameter(i, values[i]);
}
}
return queryObject.executeUpdate();
}
});
}

JdbcTemplate batchUpdate(看起来)正在执行 batchUpdate

public int[] batchUpdate(final String[] sql) throws DataAccessException {
Assert.notEmpty(sql, "SQL array must not be empty");
if (logger.isDebugEnabled()) {
logger.debug("Executing SQL batch update of " + sql.length + " statements");
}
class BatchUpdateStatementCallback implements StatementCallback<int[]>, SqlProvider {
private String currSql;
public int[] doInStatement(Statement stmt) throws SQLException, DataAccessException {
int[] rowsAffected = new int[sql.length];
if (JdbcUtils.supportsBatchUpdates(stmt.getConnection())) {
for (String sqlStmt : sql) {
this.currSql = sqlStmt;
stmt.addBatch(sqlStmt);
}
rowsAffected = stmt.executeBatch();
}
else {
for (int i = 0; i < sql.length; i++) {
this.currSql = sql[i];
if (!stmt.execute(sql[i])) {
rowsAffected[i] = stmt.getUpdateCount();
}
else {
throw new InvalidDataAccessApiUsageException("Invalid batch SQL statement: " + sql[i]);
}
}
}
return rowsAffected;
}
public String getSql() {
return this.currSql;
}
}
return execute(new BatchUpdateStatementCallback());
}

最佳答案

是的,它正在进行批量更新。如您所见,在 bulkUpdate 方法中执行的 DELETEINSERT 查询可以影响多行。这就是它们被称为批量操作的原因。

关键是要有方便的方法来执行更新和执行查询并返回批量操作中受影响的行数。此外,它包装了 DataAccessException 的异常。

关于sql - hibernatetemplate 的批量更新有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11235587/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com