SpringBoot+MyBatis通过ScriptRunner读取SQL文件

@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();
        }

    }
}

 

posted @ 2021-01-02 11:25  纵码万水千山  阅读(985)  评论(0编辑  收藏  举报