@Component public class InitDBTables implements CommandLineRunner { @Autowired DataSource dataSource; //注入DataSource public void run(String... args) { ScriptRunner runner; try { runner = new ScriptRunner(dataSource.getConnection()); runner.setAutoCommit(true); runner.setStopOnError(true); //sql文件在scr/main/resources/scripts文件夹中 runner.runScript(Resources.getResourceAsReader("scripts/yourschema.sql")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }