Populate database with hardcoded data, when Spring application starts up
1. Create data.sql script with your data
- src/main/resources/script/data.sql :
- Example data.sql script data:
2. Application.yml
3. Database @Configuration file
@Configuration @EnableJpaRepositories("com.yourproj.repository") @EnableTransactionManagement public class DatabaseConfiguration { @Autowired DataSource dataSource; public DatabaseConfiguration(Environment env) { this.env = env; } @Bean public DataSourceInitializer dataSourceInitializer() { ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator(); resourceDatabasePopulator.addScript(new ClassPathResource("/script/data.sql")); DataSourceInitializer dataSourceInitializer = new DataSourceInitializer(); dataSourceInitializer.setDataSource(dataSource); dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator); return dataSourceInitializer; } }
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου
What may be missing, or could get better?