From e09440ba63c77fb9c08b1c99bef72895a9ab4695 Mon Sep 17 00:00:00 2001 From: Agent Date: Tue, 24 Mar 2026 14:15:51 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E8=87=AA=E5=8A=A8=E5=BB=BA=E8=A1=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../building/config/DatabaseInitRunner.java | 68 +++++++++++++++++++ src/main/resources/application.yml | 3 + 2 files changed, 71 insertions(+) create mode 100644 src/main/java/com/example/building/config/DatabaseInitRunner.java diff --git a/src/main/java/com/example/building/config/DatabaseInitRunner.java b/src/main/java/com/example/building/config/DatabaseInitRunner.java new file mode 100644 index 0000000..58e0d85 --- /dev/null +++ b/src/main/java/com/example/building/config/DatabaseInitRunner.java @@ -0,0 +1,68 @@ +package com.example.building.config; + +import com.example.building.mapper.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.core.io.ClassPathResource; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; + +/** + * 数据库初始化组件 + * 启动时自动执行 SQL 脚本建表 + */ +@Component +public class DatabaseInitRunner implements CommandLineRunner { + + @Autowired + private JdbcTemplate jdbcTemplate; + + @Override + public void run(String... args) throws Exception { + try { + // 检查表是否存在 + Integer count = jdbcTemplate.queryForObject( + "SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'users'", + Integer.class + ); + + if (count == null || count == 0) { + System.out.println("=== 初始化数据库表结构 ==="); + executeSqlScript("sql/init.sql"); + System.out.println("=== 数据库初始化完成 ==="); + } + } catch (Exception e) { + System.out.println("数据库初始化跳过: " + e.getMessage()); + } + } + + private void executeSqlScript(String scriptPath) { + try { + ClassPathResource resource = new ClassPathResource(scriptPath); + String sql = new BufferedReader( + new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8) + ).lines().collect(Collectors.joining("\n")); + + // 按分号分割执行 + String[] statements = sql.split(";"); + for (String stmt : statements) { + String trimmed = stmt.trim(); + if (!trimmed.isEmpty() && !trimmed.startsWith("--")) { + try { + jdbcTemplate.execute(trimmed); + } catch (Exception e) { + // 忽略单个语句错误(如主键重复) + System.out.println("执行SQL跳过: " + e.getMessage()); + } + } + } + } catch (Exception e) { + System.err.println("执行SQL脚本失败: " + e.getMessage()); + } + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 78bdc8e..473db16 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -37,6 +37,9 @@ mybatis-plus: logic-delete-field: status logic-delete-value: 0 logic-not-delete-value: 1 + # 自动建表 + mapper-locations: classpath*:/mapper/**/*.xml + type-aliases-package: com.example.building.entity # JWT配置 - 环境变量注入 jwt: