1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/**
* 初始化 SQL 配置
*
* @author Gear
* @date 2025/03/21
*/
@Slf4j
@Configuration
@RequiredArgsConstructor
public class InitSqlConfig {
private final JdbcTemplate jdbcTemplate;
@PostConstruct
public void init() {
log.info("开始初始化 sql 数据……");
jdbcTemplate.execute("CREATE EXTENSION IF NOT EXISTS postgis;");
SqlRowSet sqlRowSet = jdbcTemplate.queryForRowSet("SELECT true FROM information_schema.tables WHERE table_name = 'table_name' limit 1;");
if (!sqlRowSet.next()) {
log.info("创建 table_name");
URL resource = InitSqlConfig.class.getResource("/sql/table_name_init.sql");
String sql = FileUtil.readString(resource, Charset.defaultCharset());
jdbcTemplate.execute(sql);
log.info("导入数据 table_name");
resource = InitSqlConfig.class.getResource("/sql/table_name.sql");
sql = FileUtil.readString(resource, Charset.defaultCharset());
jdbcTemplate.execute(sql);
}
log.info("初始化 sql 数据完成!");
}
}
|