Java develops a method for intelligently updating the List<YourEntity> list with List<String> as the filter condition (with actual combat code)

text:

In actual software development projects, it is often necessary to update and maintain the developer list. This article will introduce how to use Java to write an intelligent method to perform batch deletion and addition operations on the developer list according to requirements. At the same time, for the convenience of understanding, we will attach specific code examples.

background

Let's say we have a list that stores developer user ids userList, and a database table stProjectDevelopmentsthat contains records related to developers. Our goal is to keep the record consistent with the user ID in userList, based on updates from .stProjectDevelopmentsuserList

1. Solutions

We will implement this smart update method as follows:

1. Get an existing record

First, we use a database query operation stProjectDevelopmentsto get all the records from the table and store them in stProjectDevelopmentsa list.

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

2. Delete redundant users

Next, we need to delete redundant records in the database, i.e. users that exist in stProjectDevelopmentsthe list but don't . userListTo achieve this, we compare existingUserIds( stProjectDevelopmentslist of userids extracted from list) and userList, and find out which list of userids needs to be deleted userIdsToDelete.

Here is a code example to delete redundant users:

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. Add missing users

Now, we need to add users userListthat are present in but stProjectDevelopmentsmissing from the list. Similar to the delete operation, we compare userListwith existingUserIdsand find out the list of user IDs that need to be added userIdsToAdd.

Here's a code example to add a missing user:

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

2. Code example

Here's a complete example that demonstrates how to wrap the above solution into a single method:

/**
  * 筛选修改的开发人员
  *
  * @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);
    }
}

Summarize

In actual project development, this intelligent list update method can help us deal with developer changes more efficiently and maintain the consistency of database records. Using the sample code, you can better understand and apply the solutions in this article.

Hope this article helps you to write an efficient and smart developer list update method!

Guess you like

Origin blog.csdn.net/Da_zhenzai/article/details/131694092