Spring執(zhí)行sql腳本文件的方法
本篇解決 Spring 執(zhí)行SQL腳本(文件)的問(wèn)題。
場(chǎng)景描述可以不看。
場(chǎng)景描述:
我在運(yùn)行單測(cè)的時(shí)候,也就是 Spring 工程啟動(dòng)的時(shí)候,Spring 會(huì)去執(zhí)行 classpath:schema.sql(后面會(huì)解釋?zhuān)?,我想利用這一點(diǎn),解決一個(gè)問(wèn)題:
一次運(yùn)行多個(gè)測(cè)試文件,每個(gè)文件先后獨(dú)立運(yùn)行,而上一個(gè)文件創(chuàng)建的數(shù)據(jù),會(huì)對(duì)下一個(gè)文件運(yùn)行時(shí)造成影響,所以我要在每個(gè)文件執(zhí)行完成之后,重置數(shù)據(jù)庫(kù),不單單是把數(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 會(huì)去執(zhí)行 classpath:schema.sql,可以參考源碼
org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer#runSchemaScripts
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() + ")");
}
}
}
/**
* 默認(rèn)拿 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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java字符流和字節(jié)流對(duì)文件操作的區(qū)別
本篇文章主要介紹了Java的IO流分為字符流(Reader,Writer)和字節(jié)流(InputStream,OutputStream),字節(jié)流顧名思義字節(jié)流就是將文件的內(nèi)容讀取到字節(jié)數(shù)組,對(duì)初學(xué)者很有用,有需要的朋友可以了解一下。2016-10-10
Springboot應(yīng)用中Mybatis輸出SQL日志的3種方法代碼示例
在前臺(tái)請(qǐng)求數(shù)據(jù)的時(shí)候,sql語(yǔ)句一直都是打印到控制臺(tái)的,有一個(gè)想法就是想讓它打印到日志里,該如何做呢?這篇文章主要給大家介紹了關(guān)于Springboot應(yīng)用中Mybatis輸出SQL日志的3種方法,需要的朋友可以參考下2024-01-01
詳解Spring mvc的web.xml配置說(shuō)明
本篇文章主要介紹了Spring mvc的web.xml配置說(shuō)明,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
springboot themaleaf 第一次進(jìn)頁(yè)面不加載css的問(wèn)題
這篇文章主要介紹了springboot themaleaf 第一次進(jìn)頁(yè)面不加載css的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Intellij IDEA使用restclient測(cè)試的教程圖解
這篇文章主要介紹了Intellij IDEA使用restclient測(cè)試的教程圖解,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
基于spring三方包類(lèi)注入容器的四種方式小結(jié)
這篇文章主要介紹了基于spring三方包類(lèi)注入容器的四種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
mybatis 字段名自動(dòng)轉(zhuǎn)小寫(xiě)的實(shí)現(xiàn)
這篇文章主要介紹了mybatis 字段名自動(dòng)轉(zhuǎn)小寫(xiě)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
SpringCloud實(shí)現(xiàn)Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)
這篇文章主要介紹了SpringCloud如何實(shí)現(xiàn)Eureka服務(wù)注冊(cè)與發(fā)現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用SpringCloud,感興趣的朋友可以了解下2021-05-05
Callable實(shí)現(xiàn)多線(xiàn)程步驟詳解
這篇文章主要介紹了Callable實(shí)現(xiàn)多線(xiàn)程步驟詳解,Callable是一個(gè)接口,用于實(shí)現(xiàn)多線(xiàn)程,與實(shí)現(xiàn)Runnable類(lèi)似,但是功能更強(qiáng)大,該方法可以在任務(wù)結(jié)束后提供一個(gè)返回值,需要的朋友可以參考下2023-10-10

