本篇解决 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 处理器@componentpublicclassschemahandler {privatefinalstring schema_sql = "classpath:schema.sql";@autowiredprivatedatasource datasource;@autowiredprivatespringcontextgetter springcontextgetter;publicvoidexecute() throwsexception {resource resource = springcontextgetter.getapplicationcontext().getresource(schema_sql);scriptutils.executesqlscript(datasource.getconnection(), resource);}}// 获取 applicationcontext@componentpublicclassspringcontextgetter implementsapplicationcontextaware {privateapplicationcontext applicationcontext;publicapplicationcontext getapplicationcontext() {returnapplicationcontext;}@overridepublicvoidsetapplicationcontext(applicationcontext applicationcontext) throwsbeansexception {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
						 | privatevoidrunschemascripts() {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(newdatasourceinitializedevent(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*/privatelist<resource> getscripts(string propertyname, list<string> resources,string fallback) {if(resources != null) {returngetresources(propertyname, resources, true);}string platform = this.properties.getplatform();list<string> fallbackresources = newarraylist<string>();fallbackresources.add("classpath*:"+ fallback + "-"+ platform + ".sql");fallbackresources.add("classpath*:"+ fallback + ".sql");returngetresources(propertyname, fallbackresources, false);} | 
参考:https://github.com/spring-projects/spring-boot/issues/9048
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://segmentfault.com/a/1190000018344940
相关文章
             猜你喜欢
        
        - 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
			TA的动态
			
		
				- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
				您的支持,是我们最大的动力!				
			
		
        热门文章
        
    
    - 
            2025-05-24 98
- 
            2025-05-29 36
- 
            2025-05-29 37
- 
            2025-05-29 94
- 
            2025-05-29 20
		热门评论
	
	 
        
 
    		 
            	 
															 
         
         
        
 
                        