diff --git a/pom.xml b/pom.xml index ae76ffc..fdbc450 100644 --- a/pom.xml +++ b/pom.xml @@ -262,6 +262,8 @@ **/**/*.j2 **/*.xml + + **/*.sql **/*.yml **/*.yaml diff --git a/src/main/java/com/example/demo/common/config/DatabaseInitializer.java b/src/main/java/com/example/demo/common/config/DatabaseInitializer.java index efeba76..e07ca86 100644 --- a/src/main/java/com/example/demo/common/config/DatabaseInitializer.java +++ b/src/main/java/com/example/demo/common/config/DatabaseInitializer.java @@ -1,49 +1,55 @@ -//package com.example.demo.common.config; -// -//import lombok.RequiredArgsConstructor; -//import lombok.extern.slf4j.Slf4j; -//import org.springframework.boot.CommandLineRunner; -//import org.springframework.core.io.ClassPathResource; -//import org.springframework.jdbc.datasource.init.ScriptUtils; -//import org.springframework.stereotype.Component; -// -//import javax.sql.DataSource; -//import java.sql.Connection; -// -///** -// * 数据库初始化配置 -// * 应用启动时自动执行数据库脚本,包括创建表结构、插入初始数据等 -// * -// * @author 岳佳君 (2025年09月25日 15:00:27) -// * @version 1.0.0 -// */ -//@Slf4j -//@Component -//@RequiredArgsConstructor -//public class DatabaseInitializer implements CommandLineRunner { -// -// private final DataSource dataSource; -// -// /** -// * 应用启动时执行数据库初始化 -// * -// * @param args 命令行参数 -// * @throws Exception 初始化过程中可能抛出的异常 -// */ -// @Override -// public void run(String... args) throws Exception { -// log.info("开始初始化数据库..."); -// -// try (Connection connection = dataSource.getConnection()) { -// // 执行数据库初始化脚本 -// ClassPathResource schemaResource = new ClassPathResource("schema.sql"); -// ScriptUtils.executeSqlScript(connection, schemaResource); -// -// log.info("数据库初始化完成"); -// } catch (Exception e) { -// log.error("数据库初始化失败: {}", e.getMessage(), e); -// throw new RuntimeException("数据库初始化失败", e); -// } -// log.info("应用启动成功...."); -// } -//} \ No newline at end of file +package com.example.demo.common.config; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.core.io.ClassPathResource; +import org.springframework.jdbc.datasource.init.ScriptUtils; +import org.springframework.stereotype.Component; + +import javax.sql.DataSource; +import java.sql.Connection; + +/** + * 数据库初始化配置 + * 应用启动时自动执行数据库脚本,包括创建表结构、插入初始数据等 + * 仅在SQLite数据源存在时生效 + * + * @author 岳佳君 (2025年09月25日 15:00:27) + * @version 1.0.0 + */ +@Slf4j +@Component +@RequiredArgsConstructor +@ConditionalOnBean(name = "sqliteDataSource") +public class DatabaseInitializer implements CommandLineRunner { + + private final static String baseURL = "script/sqlite/"; + + private final DataSource dataSource; + + /** + * 应用启动时执行数据库初始化 + * + * @param args 命令行参数 + * @throws Exception 初始化过程中可能抛出的异常 + */ + @Override + public void run(String... args) throws Exception { + log.info("开始初始化数据库..."); + + try (Connection connection = dataSource.getConnection()) { + // 执行数据库初始化脚本 + ClassPathResource schemaResource = new ClassPathResource(baseURL + "sqlite-schema.sql"); + ScriptUtils.executeSqlScript(connection, schemaResource); + + log.info("{} 数据库初始化完成", connection.getMetaData().getURL()); + } catch (Exception e) { + log.error("数据库初始化失败: {}", e.getMessage(), e); + throw new RuntimeException("数据库初始化失败", e); + } + log.info("应用启动成功...."); + } +} \ No newline at end of file diff --git a/src/main/resources/static/favicon.ico b/src/main/resources/static/favicon.ico new file mode 100644 index 0000000..1d4eba9 Binary files /dev/null and b/src/main/resources/static/favicon.ico differ