Spring執(zhí)行sql腳本文件的方法
本篇解決 Spring 執(zhí)行SQL腳本(文件)的問題。
場(chǎng)景描述可以不看。
場(chǎng)景描述:
我在運(yùn)行單測(cè)的時(shí)候,也就是 Spring 工程啟動(dòng)的時(shí)候,Spring 會(huì)去執(zhí)行 classpath:schema.sql(后面會(huì)解釋),我想利用這一點(diǎn),解決一個(gè)問題:
一次運(yùn)行多個(gè)測(cè)試文件,每個(gè)文件先后獨(dú)立運(yùn)行,而上一個(gè)文件創(chuàng)建的數(shù)據(jù),會(huì)對(duì)下一個(gè)文件運(yùn)行時(shí)造成影響,所以我要在每個(gè)文件執(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 會(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
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)解析ini文件對(duì)應(yīng)到JavaBean中
ini 文件是Initialization File的縮寫,即初始化文件,是windows的系統(tǒng)配置文件所采用的存儲(chǔ)格式。這篇文章主要介紹了通過Java實(shí)現(xiàn)解析ini文件對(duì)應(yīng)到JavaBean中,需要的可以參考一下2022-01-01Java中Stream的flatMap與map使用場(chǎng)景及區(qū)別詳解
這篇文章主要介紹了Java中Stream的flatMap與map使用場(chǎng)景及區(qū)別詳解,Stream 流式操作,一般用于操作集合即 List 一類的數(shù)據(jù)結(jié)構(gòu),簡(jiǎn)單來說 Stream 的 map 使得其中的元素轉(zhuǎn)為另一種元素的映射(map)方法,需要的朋友可以參考下2024-01-01解決springboot 獲取form-data里的file文件的問題
這篇文章主要介紹了解決springboot 獲取form-data里的file文件的問題的相關(guān)資料,這里提供了詳細(xì)的解決步驟,需要的朋友可以參考下2017-07-07Java中對(duì)象?和?json?互轉(zhuǎn)四種方式?json-lib、Gson、FastJson、Jackson
這篇文章主要介紹了Java中對(duì)象?和?json?互轉(zhuǎn)?四種方式?json-lib、Gson、FastJson、Jackson,需要的朋友可以參考下2023-11-11java中Servlet監(jiān)聽器的工作原理及示例詳解
這篇文章主要介紹了java中Servlet監(jiān)聽器的工作原理及示例詳解。Servlet監(jiān)聽器用于監(jiān)聽一些重要事件的發(fā)生,監(jiān)聽器對(duì)象可以在事情發(fā)生前、發(fā)生后可以做一些必要的處理。感興趣的可以來了解一下2020-07-07springboot?注解方式批量插入數(shù)據(jù)的實(shí)現(xiàn)
一次請(qǐng)求需要往數(shù)據(jù)庫插入多條數(shù)據(jù)時(shí),可以節(jié)省大量時(shí)間,本文主要介紹了springboot?注解方式批量插入數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03MyBatis?@Select注解介紹:基本用法與動(dòng)態(tài)SQL拼寫方式
這篇文章主要介紹了MyBatis?@Select注解介紹:基本用法與動(dòng)態(tài)SQL拼寫方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07