mybatis 差异更新法

主表酒店(blue_hotel)和子表房间(blue_hotel_room)

1.先找出要删除的房间(存在于旧房间但不在新房间中),筛选出新房间的id,然后拿新房间的id去和旧房间做过滤,最后得到结果。代码示例

java 复制代码
    /**
     * 找出需要删除的房间(存在于oldRooms但不在newRooms中)
     *
     * @param oldRooms
     * @param newRooms
     * @return
     */
    private List<BlueHotelRoom> findRoomsToDelete(List<BlueHotelRoom> oldRooms, List<BlueHotelRoom> newRooms) {
        // 收集新房间中的所有ID(使用Set提高查找效率)
        Set<Long> newRoomIds = newRooms.stream()
                .map(BlueHotelRoom::getHotelRoomId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());

        // 返回旧房间中不在新房间ID集合中的房间
        return oldRooms.stream()
                .filter(room -> !newRoomIds.contains(room.getHotelRoomId()))
                .collect(Collectors.toList());
    }

2.找出需要新增或更新的房间。将旧房间的数据转换为Map<房间ID,房间对象>的数据形式,然后拿新房间的数据做过滤,新房间中的id等于空或者不在旧房间中,证明是需要新增或者更新的房间,最后检查其它字段的数据一致,不一致则更新字段。示例代码

java 复制代码
 /**
     * 找出需要新增或更新的房间
     *
     * @param oldRooms
     * @param newRooms
     * @return
     */
    private List<BlueHotelRoom> findRoomsToAddOrUpdate(List<BlueHotelRoom> oldRooms, List<BlueHotelRoom> newRooms) {
        // 收集旧房间中的所有ID(使用Map提高查找效率)
        Map<Long, BlueHotelRoom> oldRoomMap = oldRooms.stream()
                .collect(Collectors.toMap(BlueHotelRoom::getHotelRoomId, room -> room));

        return newRooms.stream()
                .filter(room -> {
                    // 新增房间(ID为null或不在旧房间中)
                    if (room.getHotelRoomId() == null) return true;
                    if (!oldRoomMap.containsKey(room.getHotelRoomId())) return true;

                    // 检查是否需要更新(比较关键字段)
                    BlueHotelRoom oldRoom = oldRoomMap.get(room.getHotelRoomId());
                    return !Objects.equals(room.getHotelRoomName(), oldRoom.getHotelRoomName()) ||
                            !Objects.equals(room.getOrderNum(), oldRoom.getOrderNum());
                    // 添加其他需要比较的字段
                })
                .collect(Collectors.toList());
    }

3.最后更新的时候先更新主表数据,也就是酒店的数据。然后查询现有的房间数据,也就是旧的房间数据(oldRooms)新的房间数据就是传过来的房间数据(newRooms),最后调用两用上面两个方法,该删除的删除,该新增的新增。示例代码

java 复制代码
  /**
     * 修改酒店管理
     *
     * @param blueHotel 酒店管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBlueHotel(BlueHotel blueHotel) {
        blueHotel.setUpdateTime(DateUtils.getNowDate());

        List<BlueHotelRoom> newRooms = blueHotel.getBlueHotelRoomList();
        // 1. 更新主表
        int rows = blueHotelMapper.updateBlueHotel(blueHotel);

        // 2. 查询现有房间
        List<BlueHotelRoom> oldRooms = blueHotelMapper.selectBlueHotelRoomByHotelId(blueHotel.getHotelId());

        // 3. 找出需要删除的房间(存在于old但不在new中)
        List<BlueHotelRoom> toDelete = findRoomsToDelete(oldRooms, newRooms);
        toDelete.forEach(room -> {
            blueHotelMapper.deleteBlueHotelRoomByHotelRoomId(room.getHotelRoomId());
        });

        // 4. 找出需要新增/更新的项目
        List<BlueHotelRoom> toAddOrUpdate = findRoomsToAddOrUpdate(oldRooms, newRooms);
        toAddOrUpdate.forEach(room -> {
            room.setHotelId(blueHotel.getHotelId());
            if (room.getHotelRoomId() == null) {
                blueHotelMapper.insertBlueHotelRoom(room);
            } else {
                blueHotelMapper.updateBlueHotelRoom(room);
            }
        });


        return rows;
    }
相关推荐
AlienZHOU6 小时前
DeepSeek Harness 插件:HTML 实时可视化编辑
前端·agent·deepseek
St_rive8 小时前
Page Object设计模式
java·开发语言·设计模式
风流 少年8 小时前
Spring AI 2.0:SSE
java·人工智能·spring
剪刀石头布啊8 小时前
javascript手动实现继承
前端
凤山老林9 小时前
精细化流量治理:Spring Boot 动态特性开关与灰度发布体系
java·spring boot·后端
Elias不吃糖9 小时前
Langfuse 入门:Trace、Prompt、Dataset、Experiment、Evaluator
前端·python·prompt·langfuse
何以解忧,唯有..9 小时前
深入理解与应对:Redis 缓存雪崩、击穿、穿透三大经典问题
redis·缓存·mybatis
vipbic9 小时前
一个前端的 9 天重构:我是怎么用 Codex 重做航栈的
前端·javascript·后端
Eason_Lou11 小时前
【无标题】
前端·vue.js·html