spring.datasource.schema/data 根据官网深趴

大家好,我是神韵,是一个技术&生活博主。出文章目的主要是两个,一是好记忆不如烂笔头,记录总结中提高自己。二是希望我的文章可以帮到大家。欢迎来点赞打卡,你们的行动将是我无限的动力。
本篇主题是:spring.datasource.schema/data 根据官网深趴

一、参数解释

https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/common-application-properties.html

spring.datasource.data= # Data (DML) script resource references.

意思就是对表的crud操作,也就是一般是放对数据的操作的路径,当然也可以放DDL语句,我经常就是这么干的,在本地测试时。DDL操作的有参数spring.datasource.schema。

二、参数介绍

首先看官网介绍
1、1.5.9版本(1.5.9后一个版本就是2.0.0)
https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/html/howto-database-initialization.html#howto-initialize-a-database-using-spring-jdbc
在这里插入图片描述
它的意思指SpringBoot 启动能从classpath路径下自动执行DDL/DML scripts,默认是两种schema.sql/data.sql,其实是四种,如果不配platform,schema-all.sql/data-all.sql也会被执行,执行顺序是先执行schema,在执行data.sql(可以制造两个error的sql在两个file,简单证明,当然你也可以跟我一样看后面提供的源码debug)。1.5.9版本的boot 源码可以在下面类中看具体执行流程org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer#runSchemaScripts。
小总结:
在boot启动时
1、classpath路径下的 schema.sql/data.sql 会被自动执行(initialize是默认值)
2、classpath路径下的 schema-all.sql/data-all.sql 会被自动执行(initialize/platform是默认值)
3、指定的spring.datasource.schema/data路径 file会被自动执行(initialize是默认值)
4、platform配置为h2(默认是all),schema-h2.sql/data-h2.sql 会被自动执行,当然可以设置为schema- p l a t f o r m . s q l / d a t a − {platform}.sql/data- platform.sql/data{platform}.sql 实现动态化配置。
5、initialize配置为false,有关于schema/data.sql 都不会被执行。
注意:jpa中,嵌入式数据库如h2,jpa启动如果jpa.hibernate.ddl-aoto:none,则有关于schema/data.sql 都不会被执行。对于非嵌入式默认值就是none,不影响。本人在1.5.9版本亲测结论。如果使用jpa创建表,最好使用的是import.sql去初始化数据(h2不能配置url,否则需要配套配置ddl-auto:create/create-drop),而不是使用data.sql

所有相关的参数
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties

1、spring.datasource.initialize=true # Populate the database using ‘data.sql’.
2、spring.datasource.continue-on-error=false # Do not stop if an error occurs while initializing the database.
3、spring.datasource.platform=all # Platform to use in the DDL or DML scripts (e.g. schema- p l a t f o r m . s q l o r d a t a − {platform}.sql or data- platform.sqlordata{platform}.sql).
4、spring.datasource.schema= # Schema (DDL) script resource references.
5、spring.datasource.data= # Data (DML) script resource references.

jpa关键参数
spring.jpa.hibernate.ddl-auto= # DDL mode. This is actually a shortcut for the “hibernate.hbm2ddl.auto” property. Default to “create-drop” when using an embedded database, “none” otherwise.
在这里插入图片描述

2、再来看2.0.0版本介绍
https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/html/howto-database-initialization.html
在这里插入图片描述
前面介绍和1.5.9版本都差不多,但是第二段开始,加了个spring.datasource.initialization-mode参数,并且枚举值默认是embedded,意思是boot启动时嵌入式数据库默认是会执行schema/data.sql script,非潜入式数据库默认是never,是不会执行上面那些script,也就是说嵌入式数据库不影响,无需配置,但是非嵌入式数据库需要手动配置spring.datasource.initialization-mode=always才会执行初始化sql
在这里插入图片描述

3、最后看2.5.0版本介绍(更高的版本大同小异,其实看2.5.1会更好些…)
在这里插入图片描述
在这个版本,我看到,以下参数被标注过时@Deprecated,并被spring.sql.init.x取代(但是原来参数还是保留了,可以使用)
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
properties 2.5.0

1、spring.datasource.initialize
spring.sql.init.enabled
2、spring.datasource.continue-on-error
spring.sql.init.continue-on-error
3、spring.datasource.platform
spring.sql.init.platform
4、spring.datasource.schema
spring.sql.init.schema-locations
5、spring.datasource.data
spring.sql.init.data-locations
6、spring.datasource.initializationMode
spring.sql.init.mode

在这里插入图片描述
注意点:
因为jpa创建table是在执行schema/data.sql之前的,如果使用jpa.hibernate.ddl-auto: create/create-drop,则需要配置jpa.defer-datasource-initialization为true,否则会找不到table而报错。如果不想配置,可以使用import.sql做数据填充,但是也需要是打开create/create-drop.

在看版本差异官方介绍
1、1.5-2.0
在这里插入图片描述
boot 2.4升到2.5中,对于数据源这块,它提了好几个,但是没有提到boot?我就很好奇,为啥用batch来举例子而不是basic。总之就是加了initialization-mode进一步控制sql自动执行流程,不单单靠initialize控制。

2、2.4-2.5 difference
在这里插入图片描述
boot 2.4升到2.5中,对于数据源这块,第一点spring.datasource.初始化数据被标注过时了,取而代之的是spring.sql.init.,也就是说只要你的版本高于等于2.5,用到相关的配置参数,直接用spring.sql.init.xx就对了。第二点也就是spirng.sql.init.mode代替了spring.datasource.initialization-mode。第三点就是jpa初始化默认是在data.sql之前,如果你要用data.sql去填充jpa创建的表,则需要配置jpa.defer-datasource-initialization为true。

三、github 地址

https://github.com/shenyun499/gather_all_single_function/tree/boot_web_spring_datasource_project

四、总结

1、boot 2.0以下,如1.5.9版本
无论对于嵌入式还是非嵌入式数据库,schema/data.sql只要位于classpath根路径下,不用配置其他东西,启动boot默认就会加载

2、boot2.0版本及以上,如2.0.0版本
这个版本引入了参数spring.datasource.initializationMode(默认参数是embedded,可选always和never)进一步控制启动是否执行sql。对于嵌入式数据库(如h2),不需要配置其它东西,默认还是会被加载,但是对于非嵌入式数据库(如MySQL、MariaDB),必须要配置spring.datasource.initializationMode为always才能在boot启动时加载schema/data.sql文件。

3、boot 5.0版本及以上,原先的spring.datasource.initializationMode已经被标注过时了,但是还是可用,新引入了spring.sql.init.mode取代前面参数,规则还是和boot2.0版本一样。

4、如果用的是jpa创建表,即参数jpa.hibernate.ddl-auto为create/create-drop,这个时候用data.sql去填充表数据,执行data.sql会在创建表之前执行,这个是jpa 的特性,所以必须配置jpa.defer-datasource-initializationw为true,不然可以使用import.sql填充数据,这个文件是jpa初始化数据用的,前提都是jpa.hibernate.ddl-auto为create/create-drop。

本文结束。如果遇到这参数方面,有不懂可以留言,记得加上boot版本号及数据库类型…

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值