启动初始化SQLite

This commit is contained in:
yuejiajun 2025-09-29 18:15:59 +08:00
parent 699c514c41
commit f9e832139f
3 changed files with 57 additions and 49 deletions

View File

@ -262,6 +262,8 @@
<include>**/**/*.j2</include> <include>**/**/*.j2</include>
<!-- Mapper文件、XML转化文件 --> <!-- Mapper文件、XML转化文件 -->
<include>**/*.xml</include> <include>**/*.xml</include>
<!-- 初始化脚本文件 -->
<include>**/*.sql</include>
<!-- 启动配置文件 --> <!-- 启动配置文件 -->
<include>**/*.yml</include> <include>**/*.yml</include>
<include>**/*.yaml</include> <include>**/*.yaml</include>

View File

@ -1,49 +1,55 @@
//package com.example.demo.common.config; package com.example.demo.common.config;
//
//import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
//import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
//import org.springframework.core.io.ClassPathResource; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
//import org.springframework.jdbc.datasource.init.ScriptUtils; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.stereotype.Component; import org.springframework.core.io.ClassPathResource;
// import org.springframework.jdbc.datasource.init.ScriptUtils;
//import javax.sql.DataSource; import org.springframework.stereotype.Component;
//import java.sql.Connection;
// import javax.sql.DataSource;
///** import java.sql.Connection;
// * 数据库初始化配置
// * 应用启动时自动执行数据库脚本包括创建表结构插入初始数据等 /**
// * * 数据库初始化配置
// * @author 岳佳君 (2025年09月25日 15:00:27) * 应用启动时自动执行数据库脚本包括创建表结构插入初始数据等
// * @version 1.0.0 * 仅在SQLite数据源存在时生效
// */ *
//@Slf4j * @author 岳佳君 (2025年09月25日 15:00:27)
//@Component * @version 1.0.0
//@RequiredArgsConstructor */
//public class DatabaseInitializer implements CommandLineRunner { @Slf4j
// @Component
// private final DataSource dataSource; @RequiredArgsConstructor
// @ConditionalOnBean(name = "sqliteDataSource")
// /** public class DatabaseInitializer implements CommandLineRunner {
// * 应用启动时执行数据库初始化
// * private final static String baseURL = "script/sqlite/";
// * @param args 命令行参数
// * @throws Exception 初始化过程中可能抛出的异常 private final DataSource dataSource;
// */
// @Override /**
// public void run(String... args) throws Exception { * 应用启动时执行数据库初始化
// log.info("开始初始化数据库..."); *
// * @param args 命令行参数
// try (Connection connection = dataSource.getConnection()) { * @throws Exception 初始化过程中可能抛出的异常
// // 执行数据库初始化脚本 */
// ClassPathResource schemaResource = new ClassPathResource("schema.sql"); @Override
// ScriptUtils.executeSqlScript(connection, schemaResource); public void run(String... args) throws Exception {
// log.info("开始初始化数据库...");
// log.info("数据库初始化完成");
// } catch (Exception e) { try (Connection connection = dataSource.getConnection()) {
// log.error("数据库初始化失败: {}", e.getMessage(), e); // 执行数据库初始化脚本
// throw new RuntimeException("数据库初始化失败", e); ClassPathResource schemaResource = new ClassPathResource(baseURL + "sqlite-schema.sql");
// } ScriptUtils.executeSqlScript(connection, schemaResource);
// log.info("应用启动成功....");
// } log.info("{} 数据库初始化完成", connection.getMetaData().getURL());
//} } catch (Exception e) {
log.error("数据库初始化失败: {}", e.getMessage(), e);
throw new RuntimeException("数据库初始化失败", e);
}
log.info("应用启动成功....");
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 409 KiB