Spring Boot如何初始化数据

  1. Hibernate机制:classpath下的import.sql,l在ddl-auto是create和create-drop时自动执行,如果ddl-auto设置为update就不合适,所以此选项不适用于生产环境
  2. Spring JDBC默认机制: data.sql
  3. 用代码选择时机执行(一般在Fixture中执行),代码如下
Resource resource = new ClassPathResource("data/init_data.sql");
ScriptUtils.executeSqlScript(jdbcTemplate.getDataSource().getConnection(), resource);

  

参考如下:

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

http://stackoverflow.com/questions/30732314/execute-sql-file-from-spring-jdbc-template

posted @ 2017-02-23 15:45  栖息之鹰  阅读(419)  评论(0编辑  收藏  举报