启动初始化SQLite
This commit is contained in:
parent
699c514c41
commit
f9e832139f
2
pom.xml
2
pom.xml
@ -262,6 +262,8 @@
|
||||
<include>**/**/*.j2</include>
|
||||
<!-- Mapper文件、XML转化文件 -->
|
||||
<include>**/*.xml</include>
|
||||
<!-- 初始化脚本文件 -->
|
||||
<include>**/*.sql</include>
|
||||
<!-- 启动配置文件 -->
|
||||
<include>**/*.yml</include>
|
||||
<include>**/*.yaml</include>
|
||||
|
@ -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("应用启动成功....");
|
||||
// }
|
||||
//}
|
||||
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("应用启动成功....");
|
||||
}
|
||||
}
|
BIN
src/main/resources/static/favicon.ico
Normal file
BIN
src/main/resources/static/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 409 KiB |
Loading…
x
Reference in New Issue
Block a user