激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品亚洲欧美日韩精品中文字幕 | 国产成人免费高清激情视频 | 国产亚洲精品综合一区 | 美国黄色毛片女人性生活片 | 成人在线观看免费爱爱 | 亚洲一级成人 | 老师你怎么会在这第2季出现 | 精品亚洲在线 | av性色全交蜜桃成熟时 | 久久男人 | 2019亚洲日韩新视频 | 狠狠操电影 | 97黄色网| 今井夏帆av一区二区 | h色视频在线观看 | 国产做爰全免费的视频黑人 | 国产美女三级做爰 | 国产亚洲区 | 小视频免费在线观看 | 亚洲啪啪 | 色av网址 | 国产18视频 | 伦一区二区三区中文字幕v亚洲 | 1级黄色毛片 | 九九热视频这里只有精品 | 91久久久久久亚洲精品禁果 | 亚洲乱码精品久久久久 | 成人国产精品免费 | 色播视频在线播放 | 精品一区二区在线观看 | 久久国产精 | av在线免费电影 | 国产午夜精品一区二区三区在线观看 | 国产1区2区3区中文字幕 | 国产精品观看在线亚洲人成网 | 一级片在线观看 | 奇米影视在线 | 欧美精品毛片 | 亚洲午夜精品视频 | 亚洲精品无码不卡在线播放he | 好吊色欧美一区二区三区四区 |