|
@@ -3,6 +3,7 @@ package com.etotem.cfc.config;
|
|
|
import com.etotem.cfc.mapper.*;
|
|
import com.etotem.cfc.mapper.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.CommandLineRunner;
|
|
import org.springframework.boot.CommandLineRunner;
|
|
|
import org.springframework.core.io.ResourceLoader;
|
|
import org.springframework.core.io.ResourceLoader;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
@@ -25,6 +26,9 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
@Resource
|
|
@Resource
|
|
|
private ResourceLoader resourceLoader;
|
|
private ResourceLoader resourceLoader;
|
|
|
|
|
|
|
|
|
|
+ @Value("${app.init-test-data:false}")
|
|
|
|
|
+ private boolean initTestData;
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public void run(String... args) throws Exception {
|
|
public void run(String... args) throws Exception {
|
|
|
log.info("开始初始化数据库...");
|
|
log.info("开始初始化数据库...");
|
|
@@ -38,12 +42,18 @@ public class DatabaseInitializer implements CommandLineRunner {
|
|
|
// 3. 执行数据库迁移(添加新列)
|
|
// 3. 执行数据库迁移(添加新列)
|
|
|
runMigrations();
|
|
runMigrations();
|
|
|
|
|
|
|
|
- // 4. 初始化默认数据
|
|
|
|
|
- initializeDefaultData();
|
|
|
|
|
-
|
|
|
|
|
- // 5. 初始化食品相关表
|
|
|
|
|
|
|
+ // 4. 初始化食品相关表(DDL,始终执行)
|
|
|
initFoodTables();
|
|
initFoodTables();
|
|
|
|
|
|
|
|
|
|
+ // 5. 初始化测试/种子数据(仅测试/开发环境)
|
|
|
|
|
+ if (initTestData) {
|
|
|
|
|
+ log.info("init-test-data=true,开始初始化测试/种子数据...");
|
|
|
|
|
+ initializeDefaultData();
|
|
|
|
|
+ log.info("测试/种子数据初始化完成!");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.info("init-test-data=false,跳过测试/种子数据初始化(生产模式)");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
log.info("数据库初始化完成!");
|
|
log.info("数据库初始化完成!");
|
|
|
}
|
|
}
|
|
|
|
|
|