博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 执行 sql 脚本(文件)
阅读量:7104 次
发布时间:2019-06-28

本文共 2901 字,大约阅读时间需要 9 分钟。

本篇解决 Spring 执行SQL脚本(文件)的问题。

场景描述可以不看。

场景描述:

我在运行单测的时候,也就是 Spring 工程启动的时候,Spring 会去执行 classpath:schema.sql(后面会解释),我想利用这一点,解决一个问题:

一次运行多个测试文件,每个文件先后独立运行,而上一个文件创建的数据,会对下一个文件运行时造成影响,所以我要在每个文件执行完成之后,重置数据库,不单单是把数据删掉,而 schema.sql 里面有 drop table 和create table。

解决方法:

//Schema 处理器@Componentpublic 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@Componentpublic 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
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
getScripts(String propertyName, List
resources, String fallback) { if (resources != null) { return getResources(propertyName, resources, true); } String platform = this.properties.getPlatform(); List
fallbackResources = new ArrayList
(); fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql"); fallbackResources.add("classpath*:" + fallback + ".sql"); return getResources(propertyName, fallbackResources, false); }

参考:

原文链接:

转载地址:http://azuhl.baihongyu.com/

你可能感兴趣的文章
确定查询各阶段消耗的时间
查看>>
Linux学习之路(三)Shell脚本初探
查看>>
CSS-三栏响应式布局(左右固宽,中间自适应)的五种方法
查看>>
Total Commander 7.55a 个人使用设置 及 快捷键 备忘
查看>>
ROS开发文档
查看>>
mysql innodb与myisam存储引擎的区别
查看>>
Wannafly挑战赛25 A 因子 数学
查看>>
Shell命令-文件及内容处理之grep(egrep)、join
查看>>
CentOS7 防火墙
查看>>
svn项目冲突时显示无法加载项目的解决方法
查看>>
2019-4-22 jdbc学习笔记
查看>>
7 行代码优雅地实现 Excel 文件导出功能?
查看>>
thinkphp3.2.3 无法调用带下划线的模型
查看>>
RK 3299 Ubuntu 配置密钥
查看>>
react-hooks: CSSProperties
查看>>
abp 关闭审计日志
查看>>
迭代器模式 Iterator 行为型 设计模式(二十)
查看>>
解决walle报错:宿主机代码检出检测出错,请确认svn用户名密码无误
查看>>
svn使用openldap验证apache访问方式
查看>>
Linux下安装emacs-24.3
查看>>