SpringBoot+MyBatis通過ScriptRunner讀取SQL檔案
阿新 • • 發佈:2021-01-02
@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(); } } }