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

---栖息之鹰(一个外表懒洋洋的内心有激情的程序员) 此博客为笔者原著,转载时请注明出处,谢谢!
原文地址:https://www.cnblogs.com/roostinghawk/p/6433789.html