0

我有一个问题,假设我最初说 100 条记录,我在 UI 上将它们显示为用户列表。现在我已经提供了通过单击针对每条记录放置的“停用”按钮来停用数字用户的规定,然后我将列表中的所有“停用”用户捕获并将其发送到 DAO 层。[停用用户的逻辑只是将'isDeleted'标志设置为true,iesoft删除所以它和我更新我已经放入列表中的多个记录一样好],有一个简单的解决方案,编写一个for循环->迭代list-> 并为每条记录触发一个查询以将 isDeleted 标志更新为 true,但如果我说要一次删除 5000 条记录,则它不是可行的解决方案。我听说并实现了“插入”的批处理更新概念一次多条记录,但我不明白如何使用批量更新仅在一次数据库调用中更新多条记录,请帮助,插入的批量更新代码如下,

 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;
        }

    }
4

1 回答 1

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;
        }
于 2016-06-13T08:00:58.860 回答