Spring 执行 sql 脚本(文件)-sql文件
本篇解决 Spring 执行SQL脚本(文件)的问题。
场景描述可以不看。
场景描述:
我在运行单测的时候,也就是 Spring 工程启动的时候,Spring 会去执行 classpath:schema.sql(后面会解释),我想利用这一点,解决一个问题:
一次运行多个测试文件,每个文件先后独立运行,而上一个文件创建的数据,会对下一个文件运行时造成影响,所以我要在每个文件执行完成之后,重置数据库,不单单是把数据删掉,而 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;
}
}
备注:
关于为何 Spring 会去执行 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() + ")");
}
}
}
/**
* 默认拿 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);
}
参考:
相关内容
-
在wps中粘贴保留原格式化|wps中复制文件保留原
在wps中粘贴保留原格式化|wps中复制文件保留原有格式,格式化,...
-
什么是boot.ini文件错误ini的非法解决方案
什么是boot.ini文件错误ini的非法解决方案,,千奇百怪的电脑问...
-
鼠标不能拖动文件了
鼠标不能拖动文件了,鼠标,拖动,本文目录鼠标不能拖动文件了电...
-
金蝶执sql语句出错|如何处理SQLServer的错误信
金蝶执sql语句出错|如何处理SQLServer的错误信息,,1.如何处理S...
-
Outlook的PST文件损坏的修复方法
Outlook的PST文件损坏的修复方法,文件, ...
-
优化PostgreSQL中的批量更新性能
优化PostgreSQL中的批量更新性能,数据,表格,在Ubuntu 12.04上...
-
IE脚本错误如何做Web脚本错误解决技巧
IE脚本错误如何做Web脚本错误解决技巧,,这个问题是由于这样的...
-
电脑什么文件不能删除文件|电脑中哪些文件不能
电脑什么文件不能删除文件|电脑中哪些文件不能删除,,电脑中哪...
-
电脑上怎样打文件|电脑上怎样打文件符号
电脑上怎样打文件|电脑上怎样打文件符号,,电脑上怎样打文件符...
-
将wps转为EX|把WPS文件转为excel表格
将wps转为EX|把WPS文件转为excel表格,转为,如何将,wps,1.怎么...
-
电脑字幕手机怎么载入|电脑字幕手机怎么载入文
电脑字幕手机怎么载入|电脑字幕手机怎么载入文件,,电脑字幕手...
-
u盘看不到文件其他电脑可以看到|电脑里看不到u
u盘看不到文件其他电脑可以看到|电脑里看不到u盘,,1. 电脑里看...
-
为什么SQL Sever2005安装完成后用windows身份登
为什么SQL Sever2005安装完成后用windows身份登录不上去,身份...
-
联想笔记本电脑主板颜色|联想笔记本颜色配置文
联想笔记本电脑主板颜色|联想笔记本颜色配置文件,,联想笔记本...
-
电脑文件或丢失开不了机|电脑文件或丢失开不了
电脑文件或丢失开不了机|电脑文件或丢失开不了机,还没有键盘...