修复邀请码层级:Spring Boot 启动时优雅更新 InviteLevel 的秘籍!

🚀 修复邀请码层级:Spring Boot 启动时优雅更新 InviteLevel 的秘籍!🔧

大家好!👋 今天我们来聊聊如何在 Spring Boot 应用启动时自动修复邀请码的 inviteLevel 字段!🎉 如果你需要处理数据不一致问题,这篇博客会帮到你!💡

我们将展示如何通过 CommandLineRunner 在启动时调用 InviteCodeServiceupdateAllInviteLevels 方法,并分享优化技巧,包括异步执行和分页处理。📝 最后,用 Mermaid 流程图直观展示流程!🖼️


📖 背景:为什么需要修复 InviteLevel?

InviteCode 实体中的 inviteLevel 表示邀请码的“父亲层级”。例如,id = 11 的邀请码有 3 个上级(10 -> 9 -> 8),inviteLevel 应为 3。但历史数据可能不正确,我们需要在启动时修复。🔍


🛠️ 实现:启动时修复 InviteLevel

1. 核心代码:ApiApplication

通过 CommandLineRunner 在启动时调用修复方法:

@SpringBootApplication
@EnableAsync
public class ApiApplication extends SpringBootServletInitializer implements CommandLineRunner {

    private final InviteCodeService inviteCodeService;
    private final Environment env;

    public ApiApplication(InviteCodeService inviteCodeService, Environment env) {
        this.inviteCodeService = inviteCodeService;
        this.env = env;
    }

    @Override
    public void run(String... args) throws Exception {
        boolean updateEnabled = env.getProperty("update.invite.levels.enabled", Boolean.class, false);
        if (updateEnabled) {
            System.out.println("Starting to update all invite levels asynchronously... 🔄");
            inviteCodeService.updateInviteLevelsForHierarchy(null);
            System.out.println("Update all invite levels task has been scheduled. ⏳");
        } else {
            System.out.println("Invite level update is disabled. ⚙️");
        }
    }
}

2. 配置:application.yml

通过配置开关控制修复行为:

update:
  invite:
    levels:
      enabled: false  # 控制是否启用修复 ⚙️

📈 流程图:修复 InviteLevel 的过程

true
false
应用启动
读取配置: update.invite.levels.enabled
调用 updateInviteLevelsForHierarchy()
跳过修复
遍历 InviteCode 记录
计算 inviteLevel
更新数据库
修复完成 ✅
应用继续启动

🚀 优化:让修复更高效!

1. 异步执行:不阻塞启动 🚀

InviteCodeService 中异步执行:

@Async
public void updateInviteLevelsForHierarchy(Integer rootId) {
    if (rootId == null) {
        int pageSize = 1000;
        int pageNumber = 0;
        Pageable pageable = PageRequest.of(pageNumber, pageSize);
        Page<InviteCode> page;
        do {
            page = inviteCodeRepository.findAll(pageable);
            for (InviteCode inviteCode : page.getContent()) {
                int parentLevel = calculateParentLevel(inviteCode.getCreatedBy());
                inviteCode.setInviteLevel(parentLevel);
                inviteCodeRepository.save(inviteCode);
            }
            pageable = pageable.next();
            pageNumber++;
        } while (page.hasNext());
    }
}

2. 分页处理:应对大数据量 📊

使用分页查询分批处理:

@Repository
public interface InviteCodeRepository extends JpaRepository<InviteCode, Integer> {
    Page<InviteCode> findAll(Pageable pageable);
}

🎉 总结

我们实现了启动时修复 inviteLevel 的功能,并通过异步执行和分页处理优化了性能。希望这篇博客对你有帮助!💬 欢迎留言讨论!🚀

📚 参考:Spring Boot 官方文档、Mermaid 流程图。点赞和分享哦!😊

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值