小编典典

如何在使用Spring Boot时加载sql函数?

spring-boot

Spring-Boot用于的应用程序hibernate.hbm2ddl.=update

如何确保启动时执行sql脚本?因此,我想确保at FUNCTION .. LANGUAGE plpgsql在我的postgresql数据库中。


阅读 433

收藏
2020-05-30

共1个答案

小编典典

通过使用 XML

<jdbc:initialize-database data-source="dataSource">
  <jdbc:script location="classpath:com/foo/sql/db-schema.sql"/>
  <jdbc:script location="classpath:com/foo/sql/db-test-data.sql"/>
</jdbc:initialize-database>

通过使用 注释 (下面的示例应该放在您的@Configuration类中)

@Value("classpath:com/foo/sql/db-schema.sql")
private Resource schemaScript;

@Value("classpath:com/foo/sql/db-test-data.sql")
private Resource dataScript;

@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
    final DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator());
    return initializer;
}

private DatabasePopulator databasePopulator() {
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(schemaScript);
    populator.addScript(dataScript);
    return populator;
}
2020-05-30