成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Spring 執(zhí)行 sql 腳本(文件)

lily_wang / 2122人閱讀

摘要:本篇解決執(zhí)行腳本文件的問題。場景描述可以不看。解決方法處理器獲取備注關(guān)于為何會去執(zhí)行,可以參考源碼默認拿和參考原文鏈接

本篇解決 Spring 執(zhí)行SQL腳本(文件)的問題。

場景描述可以不看。

場景描述:

我在運行單測的時候,也就是 Spring 工程啟動的時候,Spring 會去執(zhí)行 classpath:schema.sql(后面會解釋),我想利用這一點,解決一個問題:

一次運行多個測試文件,每個文件先后獨立運行,而上一個文件創(chuàng)建的數(shù)據(jù),會對下一個文件運行時造成影響,所以我要在每個文件執(zhí)行完成之后,重置數(shù)據(jù)庫,不單單是把數(shù)據(jù)刪掉,而 schema.sql 里面有 drop table 和create table。

解決方法:

//Schema 處理器
@Component
public class SchemaHandler {
    private final String SCHEMA_SQL = "classpath:schema.sql";
    @Autowired
    private DataSource datasource;
    @Autowired
    private SpringContextGetter springContextGetter;

    public void execute() throws Exception {
        Resource resource = springContextGetter.getApplicationContext().getResource(SCHEMA_SQL);
        ScriptUtils.executeSqlScript(datasource.getConnection(), resource);
    }
}

// 獲取 ApplicationContext
@Component
public class SpringContextGetter implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}

備注:

關(guān)于為何 Spring 會去執(zhí)行 classpath:schema.sql,可以參考源碼

org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer#runSchemaScripts

private void runSchemaScripts() {
        List scripts = getScripts("spring.datasource.schema",
                this.properties.getSchema(), "schema");
        if (!scripts.isEmpty()) {
            String username = this.properties.getSchemaUsername();
            String password = this.properties.getSchemaPassword();
            runScripts(scripts, username, password);
            try {
                this.applicationContext
                        .publishEvent(new DataSourceInitializedEvent(this.dataSource));
                // The listener might not be registered yet, so don"t rely on it.
                if (!this.initialized) {
                    runDataScripts();
                    this.initialized = true;
                }
            }
            catch (IllegalStateException ex) {
                logger.warn("Could not send event to complete DataSource initialization ("
                        + ex.getMessage() + ")");
            }
        }
    }

/**
 * 默認拿 classpath*:schema-all.sql 和 classpath*:schema.sql
 */
private List getScripts(String propertyName, List resources,
            String fallback) {
        if (resources != null) {
            return getResources(propertyName, resources, true);
        }
        String platform = this.properties.getPlatform();
        List fallbackResources = new ArrayList();
        fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql");
        fallbackResources.add("classpath*:" + fallback + ".sql");
        return getResources(propertyName, fallbackResources, false);
    }

參考:https://github.com/spring-pro...

原文鏈接:
http://zhige.me/2019/02/28/20...

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/73482.html

相關(guān)文章

  • Spring MVC實現(xiàn)Spring Security,Spring Stomp websocket

    摘要:使用框架各個組件實現(xiàn)一個在線聊天網(wǎng)頁,當有用戶連接,服務器監(jiān)聽到用戶連接會使用推送最新用戶列表,有用戶斷開刷新在線列表,實時推送用戶聊天信息。根據(jù)請求頭是否等于判斷是否是。 使用Spring框架各個組件實現(xiàn)一個在線聊天網(wǎng)頁,當有用戶連接WebSocket,服務器監(jiān)聽到用戶連接會使用Stomp推送最新用戶列表,有用戶斷開刷新在線列表,實時推送用戶聊天信息。引入Jetty服務器,直接嵌入整...

    shuibo 評論0 收藏0
  • Spring-Boot學習筆記

    摘要:學習筆記使用很容易創(chuàng)建一個獨立運行運行內(nèi)嵌容器準生產(chǎn)級別的基于框架的項目,使用你可以不用或者只需要很少的配置。異常消息如果這個錯誤是由異常引起的。錯誤發(fā)生時請求的路徑。 Spring-Boot 1.5 學習筆記 使用Spring Boot很容易創(chuàng)建一個獨立運行(運行jar,內(nèi)嵌Servlet容器)、準生產(chǎn)級別的基于Spring框架的項目,使用Spring Boot你可以不用或者只需要很...

    curlyCheng 評論0 收藏0
  • SpringSpring Boot和TestNG測試指南 - 測試關(guān)系型數(shù)據(jù)庫

    摘要:地址提供了對的支持,能夠讓我們很方便對關(guān)系型數(shù)據(jù)庫做集成測試。如果想要在打包的時候跳過集成測試,只需要。例子使用因為使用了來做集成測試,得益于其機制,不需要自己構(gòu)建和的。 Github地址 Spring Test Framework提供了對JDBC的支持,能夠讓我們很方便對關(guān)系型數(shù)據(jù)庫做集成測試。 同時Spring Boot提供了和Flyway的集成支持,能夠方便的管理開發(fā)過程中產(chǎn)生...

    Meils 評論0 收藏0

發(fā)表評論

0條評論

lily_wang

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<