Java开发以List<String>为筛选条件智能更新List<YourEntity>列表的方法(附带实战代码)

正文:

在实际的软件开发项目中,经常需要对开发人员列表进行更新和维护。本文将介绍如何使用Java编写一个智能的方法,根据需求对开发人员列表进行批量删除和新增操作。同时,为了方便理解,我们将附带具体的代码示例。

背景

假设我们有一个存储开发人员用户ID的列表userList,以及一个数据库表stProjectDevelopments,其中包含了与开发人员相关的记录。我们的目标是根据userList的更新,使得stProjectDevelopments的记录与userList中的用户ID保持一致。

一、解决方案

我们将按照以下步骤实现该智能更新方法:

1.获取现有记录

首先,我们使用数据库查询操作,从stProjectDevelopments表中获取所有记录,并将其存储在stProjectDevelopments列表中。

//查询该项目下的所有开发人员
LambdaQueryWrapper<StProjectDevelopment> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(StProjectDevelopment::getProjectId,projectId);
List<StProjectDevelopment> stProjectDevelopments = stProjectDevelopmentMapper.selectList(wrapper);

2. 删除多余的用户

接下来,我们需要删除数据库中多余的记录,即存在于stProjectDevelopments列表但不存在于userList的用户。为了实现这一点,我们比较existingUserIds(从stProjectDevelopments列表中提取的用户ID列表)和userList,并找出需要删除的用户ID列表userIdsToDelete

以下是删除多余用户的代码示例:

List<StProjectDevelopment> developmentsToDelete = stProjectDevelopments.stream()
    .filter(development -> !userList.contains(development.getUserId()))
    .collect(Collectors.toList());

if (!developmentsToDelete.isEmpty()) {
    
    
    List<Integer> idsToDelete = developmentsToDelete.stream()
        .map(StProjectDevelopment::getId)
        .collect(Collectors.toList());

    stProjectDevelopmentMapper.deleteBatchIds(idsToDelete);
}

3. 新增缺少的用户

现在,我们需要新增userList中存在但在stProjectDevelopments列表中缺失的用户。类似于删除操作,我们比较userListexistingUserIds,并找出需要新增的用户ID列表userIdsToAdd

以下是新增缺少用户的代码示例:

List<String> userIdsToAdd = userList.stream()
    .filter(userId -> !existingUserIds.contains(userId))
    .collect(Collectors.toList());

if (!userIdsToAdd.isEmpty()) {
    
    
    List<StProjectDevelopment> newDevelopments = userIdsToAdd.stream()
        .map(userId -> {
    
    
            StProjectDevelopment development = new StProjectDevelopment();
            development.setUserId(userId);
            // 设置其他字段的初始值
            return development;
        })
        .collect(Collectors.toList());

    stProjectDevelopmentMapper.insertBatch(newDevelopments);
}

二、代码示例

下面是一个完整的示例,演示了如何将以上解决方案封装为一个方法:

/**
  * 筛选修改的开发人员
  *
  * @param userList  原项目开发用户列表
  * @param projectId 项目ID
  * @author yangz
  * @date 2023/07/12
  */
public void updateStProjectDevelopments(List<String> userList,String projectId, String currentUserId) {
    
    
    //查询该项目下的所有开发人员
    LambdaQueryWrapper<StProjectDevelopment> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(StProjectDevelopment::getProjectId,projectId);
    List<StProjectDevelopment> stProjectDevelopments = stProjectDevelopmentMapper.selectList(wrapper);

    //筛选出所有的userId列表
    List<String> existingUserIds = stProjectDevelopments.stream()
        .map(StProjectDevelopment::getUserId)
        .collect(Collectors.toList());

    // 删除多余的用户
    List<StProjectDevelopment> developmentsToDelete = stProjectDevelopments.stream()
        .filter(development -> !userList.contains(development.getUserId()))
        .collect(Collectors.toList());
    if (!developmentsToDelete.isEmpty()) {
    
    
        List<String> userIdsToDelete = developmentsToDelete.stream()
            .map(StProjectDevelopment::getId)
            .collect(Collectors.toList());
        stProjectDevelopmentMapper.deleteBatchIds(userIdsToDelete);
    }

    // 新增缺少的用户
    List<String> userIdsToAdd = userList.stream()
        .filter(userId -> !existingUserIds.contains(userId))
        .collect(Collectors.toList());
    if (!userIdsToAdd.isEmpty()) {
    
    
        List<StProjectDevelopment> newDevelopments = userIdsToAdd.stream()
            .map(userId -> {
    
    
                StProjectDevelopment development = new StProjectDevelopment();
                //对象参数赋值
                stProjectDevelopmentSetParam(currentUserId, projectId, userId, development);
                return development;
            })
            .collect(Collectors.toList());
        stProjectDevelopmentMapper.batchInsert(newDevelopments);
    }
}

总结

在实际项目开发中,这种智能的列表更新方法可以帮助我们更高效地处理开发人员的变动,保持数据库记录的一致性。使用示例代码,您可以更好地理解和应用本文中的解决方案。

希望本文对您有所帮助,使您能够编写出高效、智能的开发人员列表更新方法!

猜你喜欢

转载自blog.csdn.net/Da_zhenzai/article/details/131694092