From b2d7bc094a8a0f1a7a96968888521cdd6e8a32bd Mon Sep 17 00:00:00 2001 From: AlexBob Date: Thu, 20 Feb 2025 16:21:33 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(`BootApplication.java`):=20Add?= =?UTF-8?q?=20`AppRunner`=20component=20for=20batch=20user=20data=20insert?= =?UTF-8?q?ion=20using=20`DatabaseClient`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/plate/boot/BootApplication.java | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/boot/platform/src/main/java/com/plate/boot/BootApplication.java b/boot/platform/src/main/java/com/plate/boot/BootApplication.java index 6c60a02b..6d620fcc 100644 --- a/boot/platform/src/main/java/com/plate/boot/BootApplication.java +++ b/boot/platform/src/main/java/com/plate/boot/BootApplication.java @@ -1,7 +1,11 @@ package com.plate.boot; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.r2dbc.core.DatabaseClient; +import org.springframework.stereotype.Component; /** * Entry point for the Spring Boot application. @@ -9,7 +13,7 @@ * This class serves as the primary class to launch the Spring Boot application. * It is annotated with `@SpringBootApplication`, which is a convenience annotation * that includes `@Configuration`, `@EnableAutoConfiguration`, and `@ComponentScan`. - * These annotations together configure the application context, enable auto-configuration + * These annotations together configure the application context, enable autoconfiguration * of Spring features, and scan for Spring components in the package where this class is located * and its sub-packages. *

@@ -22,4 +26,57 @@ public class BootApplication { public static void main(String[] args) { SpringApplication.run(BootApplication.class, args); } + + @Component + static class AppRunner implements ApplicationRunner { + + private final DatabaseClient databaseClient; + + AppRunner(DatabaseClient databaseClient) { + this.databaseClient = databaseClient; + } + + @Override + public void run(ApplicationArguments args) { + String sql = """ + do $$ + declare + v_code text; + v_phone text; + batch_size int := 1000; -- 每批提交的记录数 + commit_count int := 0; -- 记录已提交的批次数 + begin + for i in 1..100000 + loop + -- 生成用户代码 + if i < 1000 then + v_code := 'U' || lpad(i::text, 4, '0'); + else + v_code := 'U' || (i + 3)::text; + end if; + + -- 生成手机号码 + v_phone := '1708911826' || lpad(i::text, 2, '0'); + + -- 插入用户数据 + insert into se_users(code, username, password, name, phone, email, bio, creator, updater) + values (gen_random_uuid(), 'user' || i, + '{pbkdf2}7d8a68bc5d507bd19bc153ff10bcdef66f5a5f3d0c1ab2438630e50b5c65894bccc2c7e4404c5afa', + '普通用户' || i, v_phone, 'user' || i || '@qq.com', null, + 'f47ac10b-58cc-4372-a567-0e02b2c3d479', 'f47ac10b-58cc-4372-a567-0e02b2c3d479'); + + -- 每插入 batch_size 条记录后提交事务 + commit_count := commit_count + 1; + if commit_count % batch_size = 0 then + commit; + end if; + end loop; + + -- 提交剩余的记录 + commit; + end $$; + """; + databaseClient.sql(() -> sql).fetch().rowsUpdated().subscribe(); + } + } } \ No newline at end of file