前几天Spring Boot 2.5.0发布了,其中提到了关于Datasource初始化机制的调整,有读者私信想了解这方面作了什么调整。那么今天就要详细说说这个从新设计的配置内容,并结合实际状况说说个人理解和实践建议。java
先来纠正一个误区。主要以前在版本更新介绍的时候,存在一些表述上的问题。致使部分读者认为此次的更新是Datasource自己初始化的调整,但其实并非。此次从新设计的只是对Datasource脚本初始化机制的从新设计。mysql
先来看看此次被弃用部分的内容(位于org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
),若是你有用过这些配置内容,那么新配置就很容易理解了。git
/** * 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;
对应到配置文件里的属性以下(这里仅列出部分,就不所有列出了,主要就是对应上面源码中的属性):github
spring.datasource.schema= spring.datasource.schema-username= spring.datasource.schema-password= ...
这些配置主要用来指定数据源初始化以后要用什么用户、去执行哪些脚本、遇到错误是否继续等功能。spring
Spring Boot 2.5.0开始,启用了全新的配置方式,咱们能够从这个类org.springframework.boot.autoconfigure.sql.init.SqlInitializationProperties
里看到详情。sql
下面咱们经过一个简单的例子来体验这个功能的做用。数据库
<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 '用户表';
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`;因此,上面的例子第二次执行的时候会报错并启动失败,由于第一次执行的时候表已经存在。关于这些配置的应用,相信聪明的你必定会把它与数据库的版本管理联系起来(由于能够自动的执行脚本)。安全
那么依靠这些配置,是否能够胜任业务应用部署时候数据库初始化的自动化实现呢?
我的认为就上述所介绍的配置,虽然具有了必定的自动执行能力。但因为缺失对当前环境的判断能力,因此要应对实际的部署场景来讲,仍是远远不够的。
若是要自动化的管理数据库表结构、初始化数据的话,个人建议是:
org.springframework.jdbc.datasource.init.DataSourceInitializer
来定义更复杂的执行逻辑。更多本系列免费教程连载「点击进入汇总目录」
本文的相关例子能够查看下面仓库中的chapter3-13
目录:
原创不易,若是您以为本文不错,欢迎Star
支持,您的关注是我坚持的动力!
欢迎关注个人公众号:程序猿DD,分享其余地方看不到的知识与思考