我有一个问题,假设我最初说过100条记录,并且我在UI上将它们显示为用户列表。现在,我已经通过单击"停用"来禁用号码用户。对每一条记录放置的按钮,然后我捕获所有"停用的"列表中的用户并将其发送到DAO层。[停用用户的逻辑只是设置' isDeleted'标志为true,iesoft删除所以它就像我正在更新我已放入列表中的Ids的多个记录一样好,有一个简单的解决方案,写一个for循环 - >遍历列表 - >并且为每个记录触发一个查询,将isDeleted标志更新为true,但如果我说一次要删除5000条记录,那么它是不可行的解决方案。我一次听到并实现了"插入" 多个记录的batchUpdate概念,但我不明白如何使用批量更新仅在一次数据库调用时更新多个记录,请help,插入的批量更新代码如下,
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;
}
}
答案 0 :(得分:0)
嘿,我找到了解决方案,这就是我所做的,
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;
}