【问题标题】:batch update for updating multiple records in spring mvc with mysql用mysql批量更新spring mvc中的多条记录
【发布时间】:2016-06-11 13:40:35
【问题描述】:

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

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

    }

【问题讨论】:

    标签: mysql spring-mvc batch-updates


    【解决方案1】:

    嘿,我找到了解决方案,这就是我所做的,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 2018-09-14
      • 1970-01-01
      • 2013-07-11
      • 2018-05-28
      • 2014-06-21
      • 1970-01-01
      相关资源
      最近更新 更多