一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring執行sql腳本文件的方法

Spring執行sql腳本文件的方法

2021-07-17 13:17志哥的成長筆記 Java教程

這篇文章主要介紹了Spring執行sql腳本文件的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本篇解決 spring 執行sql腳本(文件)的問題。

場景描述可以不看。

場景描述:

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

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

解決方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//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;
  }
}

備注:

關于為何 spring 會去執行 classpath:schema.sql,可以參考源碼

org.springframework.boot.autoconfigure.jdbc.datasourceinitializer#runschemascripts

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
private void runschemascripts() {
    list<resource> 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<resource> getscripts(string propertyname, list<string> resources,
      string fallback) {
    if (resources != null) {
      return getresources(propertyname, resources, true);
    }
    string platform = this.properties.getplatform();
    list<string> fallbackresources = new arraylist<string>();
    fallbackresources.add("classpath*:" + fallback + "-" + platform + ".sql");
    fallbackresources.add("classpath*:" + fallback + ".sql");
    return getresources(propertyname, fallbackresources, false);
  }

參考:https://github.com/spring-projects/spring-boot/issues/9048

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://segmentfault.com/a/1190000018344940

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品视频免费观看 | heyzo在线观看| 亚洲a视频在线观看 | 高清国产激情视频在线观看 | 亚洲视频在线一区二区三区 | 小寡妇好紧进去了好大看视频 | 9re视频这里只有精品 | 久久久精品免费视频 | 日韩毛片免费 | 白鹿扒开内裤露出尿孔 | 闺蜜的样子小说安沁在线阅读 | 沟厕okn系列在线播放 | 色亚州 | 日本一区二区三区在线 视频 | 国产精品怡红院永久免费 | 国产一区二区免费不卡在线播放 | 色播影院性播影院私人影院 | 美女扒开胸罩露出胸大乳 | 视频高清在线观看 | 高h生子双性美人受 | 日本xxxx在线视频免费 | 欧美牛逼aa| 日本伦理动漫在线观看 | 大香人蕉免费视频75 | 91在线老王精品免费播放 | 国产精品性视频免费播放 | 久热在线这里只有精品7 | 美女脱了内裤让男生尿囗 | 白丝尤物的下面被疯狂蹂躏 | 亚洲激情网站 | 欧美日韩精品一区二区三区视频在线 | 四虎影视在线观看永久地址 | 日韩欧美一区二区不卡 | 亚洲国产免费 | 久久亚洲精品AV成人无码 | 久久亚洲国产成人影院 | 出a级黑粗大硬长爽猛视频 吃胸膜奶视频456 | 亚洲精品一区二区三区在线播放 | 色偷偷影院 | 2019自拍偷拍视频 | 男人狂擦女人的下面视频 |