35 lines
1.3 KiB
Java
35 lines
1.3 KiB
Java
package com.guwan.backend.config;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.boot.ApplicationArguments;
|
|
import org.springframework.boot.ApplicationRunner;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.core.io.ClassPathResource;
|
|
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
@Slf4j
|
|
@Configuration
|
|
@RequiredArgsConstructor
|
|
public class DatabaseInitConfig implements ApplicationRunner {
|
|
|
|
private final DataSource dataSource;
|
|
|
|
@Override
|
|
public void run(ApplicationArguments args) {
|
|
log.info("开始初始化数据库...");
|
|
try {
|
|
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
|
|
populator.addScript(new ClassPathResource("db/schema.sql"));
|
|
populator.addScript(new ClassPathResource("db/data.sql"));
|
|
// populator.addScript(new ClassPathResource("db/daaixianzun.sql"));
|
|
populator.setContinueOnError(true);
|
|
populator.execute(dataSource);
|
|
log.info("数据库初始化完成");
|
|
} catch (Exception e) {
|
|
log.error("数据库初始化失败: {}", e.getMessage());
|
|
}
|
|
}
|
|
} |