Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用?
前几天Spring Boot 2.5.0发布了,其中提到了关于Datasource初始化机制的调整,有读者私信想了解这方面做了什么调整。那么今天就要详细说说这个重新设计的配置内容,并结合实际情况说说我的理解和实践建议。
弃用内容
先来纠正一个误区。主要之前在版本更新介绍的时候,存在一些表述上的问题。导致部分读者认为这次的更新是Datasource本身初始化的调整,但其实并不是。这次重新设计的只是对Datasource脚本初始化机制的重新设计。
先来看看这次被弃用部分的内容(位于org.springframework.boot.autoconfigure.jdbc.DataSourceProperties),如果你有用过这些配置内容,那么新配置就很容易理解了。
	/**
	 * Mode to apply when determining if DataSource initialization should be performed
	 * using the available DDL and DML scripts.
	 */
	@Deprecated
	private DataSourceInitializationMode initializationMode = DataSourceInitializationMode.EMBEDDED;
	/**
	 * Platform to use in the DDL or DML scripts (such as schema-${platform}.sql or
	 * data-${platform}.sql).
	 */
	@Deprecated
	private String platform = "all";
	/**
	 * Schema (DDL) script resource references.
	 */
	private List<String> schema;
	/**
	 * Username of the database to execute DDL scripts (if different).
	 */
	@Deprecated
	private String schemaUsername;
	/**
	 * Password of the database to execute DDL scripts (if different).
	 */
	@Deprecated
	private String schemaPassword;
	/**
	 * Data (DML) script resource references.
	 */
	@Deprecated
	private List<String> data;
	/**
	 * Username of the database to execute DML scripts (if different).
	 */
	@Deprecated
	private String dataUsername;
	/**
	 * Password of the database to execute DML scripts (if different).
	 */
	@Deprecated
	private String dataPassword;
	/**
	 * Whether to stop if an error occurs while initializing the database.
	 */
	@Deprecated
	private boolean continueOnError = false;
	/**
	 * Statement separator in SQL initialization scripts.
	 */
	@Deprecated
	private String separator = ";";
	/**
	 * SQL scripts encoding.
	 */
	@Deprecated
	private Charset sqlScriptEncoding;
对应到配置文件里的属性如下(这里仅列出部分,就不全部列出了,主要就是对应上面源码中的属性):
spring.datasource.schema=
spring.datasource.schema-username=
spring.datasource.schema-password=
...
这些配置主要用来指定数据源初始化之后要用什么用户、去执行哪些脚本、遇到错误是否继续等功能。
新的设计
Spring Boot 2.5.0开始,启用了全新的配置方式,我们可以从这个类org.springframework.boot.autoconfigure.sql.init.SqlInitializationProperties里看到详情。
下面我们通过一个简单的例子来体验这个功能的作用。
- 创建一个Spring Boot的基础应用,并在pom.xml中引入和mysql的依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
- 在配置文件中增加数据源和初始化数据源的配置,具体如下:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Spring Boot 2.5.0 init schema & data
# 执行初始化脚本的用户名称
spring.sql.init.username=root
# 执行初始化脚本的用户密码
spring.sql.init.password=
# 初始化的schema脚本位置
spring.sql.init.schema-locations=classpath*:schema-all.sql
- 根据上面配置的定义,接下来就在resource目录下,创建脚本文件schema-all.sql,并写入一些初始化表结构的脚本
create table test.user_info
(
    id          int unsigned auto_increment comment '用户id'
        primary key,
    open_id     varchar(255)     default '' null comment '微信小程序openid',
    nick_name   varchar(255)     default '' null comment '微信名',
    head_img    varchar(255)     default '' null comment '微信头像',
    sex         varchar(255)     default '' null comment '性别',
    phone       varchar(255)     default '' null comment '手机',
    province    varchar(255)     default '' null comment '注册地址:省',
    city        varchar(255)     default '' null comment '注册地址:城市',
    country     varchar(255)     default '' null comment '注册地址:县/区',
    status      tinyint unsigned default 0  not null comment '是否标记删除 0:否 1:是',
    create_time datetime                    not null comment '创建时间',
    update_time datetime                    not null comment '更新时间'
)
comment '用户表';
- 完成上面步骤之后,启动应用。然后打开MySQL客户端,可以看到在test库下,多了一个user_info表
通过上面的例子,不难想到这样的功能主要可以用来管理应用启动与数据库配置的自动执行,以减少应用部署过程中手工执行的内容,降低应用部署的执行步骤。
配置详解
除了上面用到的配置属性之外,还有一些其他的配置,下面详细讲解一下作用。
- spring.sql.init.enabled:是否启动初始化的开关,默认是true。如果不想执行初始化脚本,设置为false即可。通过-D的命令行参数会更容易控制。
- spring.sql.init.username和- spring.sql.init.password:配置执行初始化脚本的用户名与密码。这个非常有必要,因为安全管理要求,通常给业务应用分配的用户对一些建表删表等命令没有权限。这样就可以与datasource中的用户分开管理。
- spring.sql.init.schema-locations:配置与schema变更相关的sql脚本,可配置多个(默认用- ;分割)
- spring.sql.init.data-locations:用来配置与数据相关的sql脚本,可配置多个(默认用- ;分割)
- spring.sql.init.encoding:配置脚本文件的编码
- spring.sql.init.separator:配置多个sql文件的分隔符,默认是- ;
- spring.sql.init.continue-on-error:如果执行脚本过程中碰到错误是否继续,默认是false`;所以,上面的例子第二次执行的时候会报错并启动失败,因为第一次执行的时候表已经存在。
应用建议
关于这些配置的应用,相信聪明的你一定会把它与数据库的版本管理联系起来(因为可以自动的执行脚本)。
那么依靠这些配置,是否可以胜任业务应用部署时候数据库初始化的自动化实现呢?
个人认为就上述所介绍的配置,虽然具备了一定的自动执行能力。但由于缺失对当前环境的判断能力,所以要应对实际的部署场景来说,还是远远不够的。
如果要自动化的管理数据库表结构、初始化数据的话,我的建议是:
- 默认提供的这个初始化功能可以且仅用于单元测试,自动创建数据库结构与初始化数据,使用完毕后销毁。可以方便的控制每次单元测试的执行环境一致。
- 应用在环境部署的时候,还是要使用之前介绍过的Flyway来实现,如何使用可见之前的分享:使用Flyway来管理数据库版本。
- 联合Flyway一同使用,通过org.springframework.jdbc.datasource.init.DataSourceInitializer来定义更复杂的执行逻辑。
更多本系列免费教程连载「点击进入汇总目录」
代码示例
本文的相关例子可以查看下面仓库中的chapter3-13目录:
- Github:https://github.com/dyc87112/SpringBoot-Learning/
- Gitee:https://gitee.com/didispace/SpringBoot-Learning/
原创不易,如果您觉得本文不错,欢迎Star支持,您的关注是我坚持的动力!
欢迎关注我的公众号:程序猿DD,分享外面看不到的干货与思考!
Spring Boot 2.5.0 重新设计的spring.sql.init 配置有啥用?的更多相关文章
- Spring Boot 2.5.0 重新设计的spring.sql.init 配置有何用?
		前几天Spring Boot 2.5.0发布了,其中提到了关于Datasource初始化机制的调整,有读者私信想了解这方面做了什么调整.那么今天就要详细说说这个重新设计的配置内容,并结合实际情况说说我 ... 
- Spring Boot 2.4.0 发布,配置文件重大调整,不要乱升级!!
		前段时间 Spring Boot 2.4.0 发布了,栈长作了一个新特性全盘解读,其中介绍了一个很重要的变革,那就是配置文件. 配置文件可是每个框架的核心,不得不搞清楚,所以,这篇栈长就带大家深入实战 ... 
- Spring Boot 2.1.0 已发布,7 个重大更新!
		距离<重磅:Spring Boot 2.0 正式发布!>已经过去大半年了,而 Spring Boot 2.1.0 在 10 月底就发布了,我们来看下 Spring Boot 2.1.0 都 ... 
- (转)Spring Boot 2 (九):【重磅】Spring Boot 2.1.0 权威发布
		http://www.ityouknow.com/springboot/2018/11/03/spring-boot-2.1.html 如果这两天登录 https://start.spring.io/ ... 
- 【重磅】Spring Boot 2.1.0 权威发布
		如果这两天登录 https://start.spring.io/ 就会发现,Spring Boot 默认版本已经升到了 2.1.0.这是因为 Spring Boot 刚刚发布了 2.1.0 版本,我们 ... 
- Spring boot 2.1.0 -- swagger2 整合
		依赖版本信息 Spring boot 2.1.0.RELEASE swagger2 2.7.0 1. mvn 配置 pom.xml 包引入 <!--swagger2依赖--> <d ... 
- Spring Boot 2.2.0新特性
		Spring Boot 2.2.0 正式发布了,可从 repo.spring.io 或是 Maven Central 获取. 性能提升 Spring Boot 2.2.0 的性能获得了很大的提升. ... 
- Spring Boot 2.2.0 正式发布,支持 JDK 13!
		Java技术栈 www.javastack.cn 优秀的Java技术公众号 推荐阅读: Spring Boot 2.2.0 正式发布了,可从 repo.spring.io 或是 Maven Centr ... 
- Spring Boot 2.3.0正式发布:优雅停机、配置文件位置通配符新特性一览
		当大潮退去,才知道谁在裸泳..关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis. ... 
随机推荐
- 对接快递100&聚水潭API
			对接快递100&聚水潭API 入我相思门,知我相思苦. 简介:对接第三方平台快递100&聚水潭API的简要总结. 1.感悟 个人感觉快递100的API更友好一些,比如有SDK可以调用: ... 
- 翻译:《实用的Python编程》08_01_Testing
			目录 | 上一节 (7.5 装饰方法 | 下一节 (8.2 日志) 8.1 测试 多测试,少调试(Testing Rocks, Debugging Sucks) Python 的动态性质使得测试对大多 ... 
- Data Mining UVA - 1591
			Dr. Tuple is working on the new data-mining application for Advanced Commercial Merchandise Inc. O ... 
- Day05_24_继承
			继承 什么是继承? 继承本质上是对某一批类的抽象,从而实现对现实世界更好的建模.继承是类和类之间的一种关系,除此之外类和类之间的关系还有依赖.组合.聚合等. 继承就是子类(派生类)继承父类(基类)的特 ... 
- D - 下个也是签到题 FZU - 2221(博弈)
			ZB loves watching RunningMan! There's a game in RunningMan called 100 vs 100. There are two teams, e ... 
- Spring Security极简入门三部曲(上篇)
			目录 Spring Security极简入门三部曲(上篇) 写在前面 为什么要用Spring Security 数据库设计 demo时刻 核心代码讲解 小结 Spring Security极简入门三部 ... 
- 基于Docker配置本地Gitlab
			技术背景 Github和Gitee(码云)是最常见的基于git的代码托管平台,现在基于svn的代码管理仓库已经相对比较少见了,大部分还都是企业内部的代码仓.但是基于开源的Gitlab,我们在企业内网也 ... 
- Python容器数据类型之间的转换
			强制转换成字符串 str() 将列表(list)转为字符串 var1 = [1, 2, 3, 4] var2 = str(var1) print(type(var2)) # <class 'st ... 
- 书评第001篇:《C++黑客编程揭秘与防范》
			本书基本信息 作者:冀云(编著) 出版社:人民邮电出版社 出版时间:2012-6-1 ISBN:9787115280640 版次:1 页数:265 字数:406000 印刷时间:2012-6-1 开本 ... 
- hdu5251最小矩形覆盖
			题意(中问题直接粘吧)矩形面积 Problem Description 小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少. Input ... 
