使用 mysql 更新 spring mvc 中的多条记录的批量更新
batch update for updating multiple records in spring mvc with mysql
我有一个问题,假设我最初说了 100 条记录,并且我在 UI 上将它们显示为用户列表。现在我已经提供了通过单击针对每个记录放置的 "deactivate" 按钮停用 number 用户的规定,然后我捕获列表中的所有 "deactivated" 用户并将其发送到 DAO 层。 [the停用用户的逻辑只是将 'isDeleted' 标志设置为 true,i.e.soft 删除所以它与更新多个记录一样好,我已将其 ID 放入列表],有一个简单的解决方案,编写一个 for 循环-> 遍历列表-> 并为每条记录触发一个查询以将 isDeleted 标志更新为 true,但如果我说要一次删除 5000 条记录,它是不可行的解决方案。我听说过并一次实现了 "Inserting" 多条记录的 batchUpdate 概念,但我不明白如何使用 batchUpdate 在一次数据库调用中更新多条记录,请求助,插入的批量更新代码如下,
private static final String INSERT_USER_PERMISSION =
"INSERT INTO permission_transaction(permissionId,userId,isDeleted) "
+ "VALUES(?,?,?)";
@Transactional
public void addPermission(final UserVO userVo, final List<PermissionVO> permissionVoList)
throws Exception {
logger.info("Adding user permission, for userId: "+userVo.getUserId());
try {
jdbc.batchUpdate(INSERT_USER_PERMISSION, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
PermissionVO permissionVo = permissionVoList.get(i);
ps.setInt(1, permissionVo.getPermissionId());
ps.setInt(2, userVo.getUserId());
ps.setBoolean(3, false);
}
@Override
public int getBatchSize() {
return permissionVoList.size();
}
});
logger.info("Exiting addPermission, for userId: "+userVo.getUserId());
}catch (Exception e) {
logger.error("Error in adding user permission: " + e.getMessage(), e);
throw e;
}
}
嘿,我找到了解决方案,这是我所做的,
private static final String UPDATE_CLIENT_OWNER =
"UPDATE clientowner SET "
+ "clientOwnerName=?,"
+ "clientOwnerPhone=?,"
+ "clientOwnerEmail=?,"
+ "lastUpdatedOn=NOW() "
+ "WHERE clientOwnerId=?";
@Transactional
public void updateClientOwner(int clientId, List<ClientOwnerVO> clientOwnerVoList) throws Exception {
logger.info("Updating client Owner(s)");
try{
int[] count = jdbc.batchUpdate(UPDATE_CLIENT_OWNER, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ClientOwnerVO clientOwnerVO = clientOwnerVoList.get(i);
ps.setString(1, clientOwnerVO.getClientOwnerName());
ps.setString(2, VariableEncryption.encrypt(clientOwnerVO.getClientOwnerPhone(), clientOwnerVO.getCreatedOn()));
ps.setString(3, VariableEncryption.encrypt(clientOwnerVO.getClientOwnerEmail(), clientOwnerVO.getCreatedOn()));
ps.setInt(4, clientOwnerVO.getClientOwnerID());
}
@Override
public int getBatchSize() {
return clientOwnerVoList.size();
}
});
logger.info("Exiting After successfully updating "+count.toString()+" client owners");
}catch (Exception e) {
logger.error("Error in updating client owners: " + e.getMessage(), e);
throw e;
}
我有一个问题,假设我最初说了 100 条记录,并且我在 UI 上将它们显示为用户列表。现在我已经提供了通过单击针对每个记录放置的 "deactivate" 按钮停用 number 用户的规定,然后我捕获列表中的所有 "deactivated" 用户并将其发送到 DAO 层。 [the停用用户的逻辑只是将 'isDeleted' 标志设置为 true,i.e.soft 删除所以它与更新多个记录一样好,我已将其 ID 放入列表],有一个简单的解决方案,编写一个 for 循环-> 遍历列表-> 并为每条记录触发一个查询以将 isDeleted 标志更新为 true,但如果我说要一次删除 5000 条记录,它是不可行的解决方案。我听说过并一次实现了 "Inserting" 多条记录的 batchUpdate 概念,但我不明白如何使用 batchUpdate 在一次数据库调用中更新多条记录,请求助,插入的批量更新代码如下,
private static final String INSERT_USER_PERMISSION =
"INSERT INTO permission_transaction(permissionId,userId,isDeleted) "
+ "VALUES(?,?,?)";
@Transactional
public void addPermission(final UserVO userVo, final List<PermissionVO> permissionVoList)
throws Exception {
logger.info("Adding user permission, for userId: "+userVo.getUserId());
try {
jdbc.batchUpdate(INSERT_USER_PERMISSION, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
PermissionVO permissionVo = permissionVoList.get(i);
ps.setInt(1, permissionVo.getPermissionId());
ps.setInt(2, userVo.getUserId());
ps.setBoolean(3, false);
}
@Override
public int getBatchSize() {
return permissionVoList.size();
}
});
logger.info("Exiting addPermission, for userId: "+userVo.getUserId());
}catch (Exception e) {
logger.error("Error in adding user permission: " + e.getMessage(), e);
throw e;
}
}
嘿,我找到了解决方案,这是我所做的,
private static final String UPDATE_CLIENT_OWNER =
"UPDATE clientowner SET "
+ "clientOwnerName=?,"
+ "clientOwnerPhone=?,"
+ "clientOwnerEmail=?,"
+ "lastUpdatedOn=NOW() "
+ "WHERE clientOwnerId=?";
@Transactional
public void updateClientOwner(int clientId, List<ClientOwnerVO> clientOwnerVoList) throws Exception {
logger.info("Updating client Owner(s)");
try{
int[] count = jdbc.batchUpdate(UPDATE_CLIENT_OWNER, new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ClientOwnerVO clientOwnerVO = clientOwnerVoList.get(i);
ps.setString(1, clientOwnerVO.getClientOwnerName());
ps.setString(2, VariableEncryption.encrypt(clientOwnerVO.getClientOwnerPhone(), clientOwnerVO.getCreatedOn()));
ps.setString(3, VariableEncryption.encrypt(clientOwnerVO.getClientOwnerEmail(), clientOwnerVO.getCreatedOn()));
ps.setInt(4, clientOwnerVO.getClientOwnerID());
}
@Override
public int getBatchSize() {
return clientOwnerVoList.size();
}
});
logger.info("Exiting After successfully updating "+count.toString()+" client owners");
}catch (Exception e) {
logger.error("Error in updating client owners: " + e.getMessage(), e);
throw e;
}