李锋镝的博客

  • 首页
  • 时间轴
  • 说说
  • 每日心情
  • Now
  • 系列文章
  • 论坛
  • 左邻右舍
    • 左邻右舍
    • 博友圈
  • 留言
    • 留言
    • 走心评论
  • 关于
    • 关于我
    • 网站地图
    • 网站统计
    • 另一个网站
    • 我的导航站
    • 赞助
  • 🚇开往
Destiny
自是人生长恨水长东
  1. 首页
  2. 原创
  3. 正文

Spring Boot 2.5.0重新设计的spring.sql.init 配置有啥用?

2021年5月29日 约 1,164 字4 分钟 48 3 0
本文最后更新于 2021年5月29日,距今已 1907 天,其中的信息可能已经发生变化,请注意甄别。

Spring Boot 2.5.0 发布:支持Java16、Gradle 7、Datasource初始化机制调整。

这次就简单说下重新设计的spring.sql.init 配置有啥用。

先来看看这次被弃用部分的内容(位于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;所以,上面的例子第二次执行的时候会报错并启动失败,因为第一次执行的时候表已经存在。
除非注明,否则均为李锋镝的博客原创文章,转载必须以链接形式标明本文链接

本文链接:https://www.lifengdi.com/article/3444

推荐阅读

  • SpringBoot整合GraphQL入门教程
  • SpringBoot常用注解
  • CompletableFuture使用详解
  • SpringBoot 中内置的 49 个常用工具类
  • SpringBoot 实现接口防刷的 5 种实现方案
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: JAVA SpringBoot SQL
最后更新:2021年5月29日
相关文章
  • 数据库中的锁-SQL Server版2019年10月9日
  • 如何查看JVM参数2020年8月3日
  • Eclipse反编译插件Jad安装2019年9月27日
  • 常用正则表达式2019年6月20日
  • Java布尔运算2022年7月18日

李锋镝

既然选择了远方,便只顾风雨兼程。

打赏 点赞
< 上一篇
下一篇 >
1234567891112131415161718192021222324252627282930313233343536373839404142434446474849505152535455575859606162636465666769727476777879808182858687909293949596979899
取消回复

文章评论

还没有评论,快来抢沙发吧~

Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma -- which is living with the results of other people's thinking. Don't let the noise of other's opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition. They somehow already know what you truly want to become. Everything else is secondary.

听点儿音乐吧 朋友~
文章目录
最新 热点 随机
最新 热点 随机
每日早报 · 2026年8月18日 写了一个订阅每日新闻的WP插件 每日早报 · 2026年8月17日 每日早报 · 2026年8月16日 每日早报 · 2026年8月15日 每日早报 · 2026年8月14日
给主题增加了Now、每日心情、年度回顾、岁月同一天、随机漫步等功能Kratos+ v1.1.16版本更新说明AI时代,个人技术博客的出路在哪里?增加了两套复古皮肤-牛皮纸、千禧网页这个域名注册整整十年了,十年时间,真快啊Kratos+ v1.1.14版本更新说明
CC知识共享协议各协议内容解释 深度解析多级缓存架构:从设计到落地,彻底解决数据一致性难题 从零开始入门 K8s | Kubernetes 网络概念及策略控制 AI原生数据库新标杆:seekdb深度解析,轻量架构与混合搜索的双重革命 结合Apollo配置中心实现日志级别动态配置 使用RocketMQ时,服务启动过程中,Consumer在服务未启动时消费消息问题处理
最近评论
李锋镝 发布于 3 小时前(08月18日) 给主题加了个功能,可以在RSS中排除指定分类,哈哈哈
李锋镝 发布于 4 小时前(08月18日) :8: 喜欢就好
不凡 发布于 14 小时前(08月17日) 哇,配色真好看,观感很舒服! :52:
李锋镝 发布于 17 小时前(08月17日) 哈哈哈,那个插件我之前也用过,那个内容都是固定的,我这个每天不同的时间拉取数据也不一样,等我研究研究...
Hary 发布于 17 小时前(08月17日) emmm大哥,这样每天输出到博客,然后别人订阅rss全是这个,有点不太好,之前很流行一段时间每天60...
标签聚合
Redis MQ JVM MySQL 多线程 ElasticSearch 日常 Claude IDEA AI 分布式 SpringBoot SQL AI编程 K8s 数据库 WordPress JAVA 架构 Spring
友情链接
  • 瓦匠个人小站
  • 若梦博客
  • 九仞之行
  • 老张博客
  • 知向前端
  • 哥斯拉
  • Honesty
  • 韩情脉脉
  • 皮皮社
  • Serendipity
  • Mr.Sun的博客
  • 韩小韩博客
  • 志文工作室
  • 懋和道人
  • 临窗旋墨
  • 搬砖日记
  • 林羽凡
  • 彬红茶日记
  • 蜗牛工作室
  • sssr7844的博客

COPYRIGHT © 2026 lifengdi.com. ALL RIGHTS RESERVED.

正在博友圈履约中

域名年龄

Theme Kratos+ By Dylan Li

津ICP备2024022503号-3

京公网安备11011502039375号