久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring Boot 2.5.0 重新設(shè)計(jì)的spring.sql.init 配置有啥用

瀏覽:72日期:2023-07-14 10:54:46
棄用內(nèi)容

先來(lái)糾正一個(gè)誤區(qū)。主要之前在版本更新介紹的時(shí)候,存在一些表述上的問(wèn)題。導(dǎo)致部分讀者認(rèn)為這次的更新是Datasource本身初始化的調(diào)整,但其實(shí)并不是。這次重新設(shè)計(jì)的只是對(duì)Datasource腳本初始化機(jī)制的重新設(shè)計(jì)。

先來(lái)看看這次被棄用部分的內(nèi)容(位于org.springframework.boot.autoconfigure.jdbc.DataSourceProperties),如果你有用過(guò)這些配置內(nèi)容,那么新配置就很容易理解了。

/** * Mode to apply when determining if DataSource initialization should be performed * using the available DDL and DML scripts. */@Deprecatedprivate DataSourceInitializationMode initializationMode = DataSourceInitializationMode.EMBEDDED;/** * Platform to use in the DDL or DML scripts (such as schema-${platform}.sql or * data-${platform}.sql). */@Deprecatedprivate String platform = 'all';/** * Schema (DDL) script resource references. */private List<String> schema;/** * Username of the database to execute DDL scripts (if different). */@Deprecatedprivate String schemaUsername;/** * Password of the database to execute DDL scripts (if different). */@Deprecatedprivate String schemaPassword;/** * Data (DML) script resource references. */@Deprecatedprivate List<String> data;/** * Username of the database to execute DML scripts (if different). */@Deprecatedprivate String dataUsername;/** * Password of the database to execute DML scripts (if different). */@Deprecatedprivate String dataPassword;/** * Whether to stop if an error occurs while initializing the database. */@Deprecatedprivate boolean continueOnError = false;/** * Statement separator in SQL initialization scripts. */@Deprecatedprivate String separator = ';';/** * SQL scripts encoding. */@Deprecatedprivate Charset sqlScriptEncoding;

對(duì)應(yīng)到配置文件里的屬性如下(這里僅列出部分,就不全部列出了,主要就是對(duì)應(yīng)上面源碼中的屬性):

spring.datasource.schema=spring.datasource.schema-username=spring.datasource.schema-password=...

這些配置主要用來(lái)指定數(shù)據(jù)源初始化之后要用什么用戶、去執(zhí)行哪些腳本、遇到錯(cuò)誤是否繼續(xù)等功能。

新的設(shè)計(jì)

Spring Boot 2.5.0開(kāi)始,啟用了全新的配置方式,我們可以從這個(gè)類org.springframework.boot.autoconfigure.sql.init.SqlInitializationProperties里看到詳情。

下面我們通過(guò)一個(gè)簡(jiǎn)單的例子來(lái)體驗(yàn)這個(gè)功能的作用。

創(chuàng)建一個(gè)Spring Boot的基礎(chǔ)應(yīng)用,并在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>

在配置文件中增加數(shù)據(jù)源和初始化數(shù)據(jù)源的配置,具體如下:

spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# Spring Boot 2.5.0 init schema & data# 執(zhí)行初始化腳本的用戶名稱spring.sql.init.username=root# 執(zhí)行初始化腳本的用戶密碼spring.sql.init.password=# 初始化的schema腳本位置spring.sql.init.schema-locations=classpath*:schema-all.sql

根據(jù)上面配置的定義,接下來(lái)就在resource目錄下,創(chuàng)建腳本文件schema-all.sql,并寫(xiě)入一些初始化表結(jié)構(gòu)的腳本

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 ’手機(jī)’, province varchar(255) default ’’ null comment ’注冊(cè)地址:省’, cityvarchar(255) default ’’ null comment ’注冊(cè)地址:城市’, country varchar(255) default ’’ null comment ’注冊(cè)地址:縣/區(qū)’, status tinyint unsigned default 0 not null comment ’是否標(biāo)記刪除 0:否 1:是’, create_time datetime not null comment ’創(chuàng)建時(shí)間’, update_time datetime not null comment ’更新時(shí)間’)comment ’用戶表’;

完成上面步驟之后,啟動(dòng)應(yīng)用。然后打開(kāi)MySQL客戶端,可以看到在test庫(kù)下,多了一個(gè)user_info表

通過(guò)上面的例子,不難想到這樣的功能主要可以用來(lái)管理應(yīng)用啟動(dòng)與數(shù)據(jù)庫(kù)配置的自動(dòng)執(zhí)行,以減少應(yīng)用部署過(guò)程中手工執(zhí)行的內(nèi)容,降低應(yīng)用部署的執(zhí)行步驟。

配置詳解

除了上面用到的配置屬性之外,還有一些其他的配置,下面詳細(xì)講解一下作用。

spring.sql.init.enabled:是否啟動(dòng)初始化的開(kāi)關(guān),默認(rèn)是true。如果不想執(zhí)行初始化腳本,設(shè)置為false即可。通過(guò)-D的命令行參數(shù)會(huì)更容易控制。 spring.sql.init.username和spring.sql.init.password:配置執(zhí)行初始化腳本的用戶名與密碼。這個(gè)非常有必要,因?yàn)榘踩芾硪?,通常給業(yè)務(wù)應(yīng)用分配的用戶對(duì)一些建表刪表等命令沒(méi)有權(quán)限。這樣就可以與datasource中的用戶分開(kāi)管理。 spring.sql.init.schema-locations:配置與schema變更相關(guān)的sql腳本,可配置多個(gè)(默認(rèn)用;分割) spring.sql.init.data-locations:用來(lái)配置與數(shù)據(jù)相關(guān)的sql腳本,可配置多個(gè)(默認(rèn)用;分割) spring.sql.init.encoding:配置腳本文件的編碼 spring.sql.init.separator:配置多個(gè)sql文件的分隔符,默認(rèn)是; spring.sql.init.continue-on-error:如果執(zhí)行腳本過(guò)程中碰到錯(cuò)誤是否繼續(xù),默認(rèn)是false`;所以,上面的例子第二次執(zhí)行的時(shí)候會(huì)報(bào)錯(cuò)并啟動(dòng)失敗,因?yàn)榈谝淮螆?zhí)行的時(shí)候表已經(jīng)存在。應(yīng)用建議

關(guān)于這些配置的應(yīng)用,相信聰明的你一定會(huì)把它與數(shù)據(jù)庫(kù)的版本管理聯(lián)系起來(lái)(因?yàn)榭梢宰詣?dòng)的執(zhí)行腳本)。

那么依靠這些配置,是否可以勝任業(yè)務(wù)應(yīng)用部署時(shí)候數(shù)據(jù)庫(kù)初始化的自動(dòng)化實(shí)現(xiàn)呢?

個(gè)人認(rèn)為就上述所介紹的配置,雖然具備了一定的自動(dòng)執(zhí)行能力。但由于缺失對(duì)當(dāng)前環(huán)境的判斷能力,所以要應(yīng)對(duì)實(shí)際的部署場(chǎng)景來(lái)說(shuō),還是遠(yuǎn)遠(yuǎn)不夠的。

如果要自動(dòng)化的管理數(shù)據(jù)庫(kù)表結(jié)構(gòu)、初始化數(shù)據(jù)的話,我的建議是:

默認(rèn)提供的這個(gè)初始化功能可以且僅用于單元測(cè)試,自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)結(jié)構(gòu)與初始化數(shù)據(jù),使用完畢后銷(xiāo)毀??梢苑奖愕目刂泼看螁卧獪y(cè)試的執(zhí)行環(huán)境一致。 應(yīng)用在環(huán)境部署的時(shí)候,還是要使用之前介紹過(guò)的Flyway來(lái)實(shí)現(xiàn),如何使用可見(jiàn)之前的分享:使用Flyway來(lái)管理數(shù)據(jù)庫(kù)版本。 聯(lián)合Flyway一同使用,通過(guò)org.springframework.jdbc.datasource.init.DataSourceInitializer來(lái)定義更復(fù)雜的執(zhí)行邏輯。

更多本系列免費(fèi)教程連載「點(diǎn)擊進(jìn)入?yún)R總目錄」

代碼示例

本文的相關(guān)例子可以查看下面?zhèn)}庫(kù)中的chapter3-13目錄:

Github:https://github.com/dyc87112/SpringBoot-Learning/

Gitee:https://gitee.com/didispace/SpringBoot-Learning/

到此這篇關(guān)于Spring Boot 2.5.0 重新設(shè)計(jì)的spring.sql.init 配置有啥用?的文章就介紹到這了,更多相關(guān)spring.sql.init 配置內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产日韩一区二区 | 精品99在线 | 精产国产伦理一二三区 | 成人国产在线 | 99在线精品视频 | 超碰人人插 | 国产乱视频网站 | 一区二区三区在线 | 久久伊人成人 | 韩国精品一区二区三区 | 欧美久久影视 | 久久国产精品毛片 | 操操操操操操操 | 久久av网址 | 日日干夜夜骑 | 欧美日韩一区二区三区 | 亚洲精品久久久久久下一站 | 国产综合久久久久久鬼色 | 日韩视频不卡 | 日韩精品一区二区三区在线播放 | www.国产.com| 中国一级大毛片 | 亚洲精品视频在线播放 | 欧美日产国产成人免费图片 | 国产99久久久久久免费看农村 | 欧美午夜一区二区三区免费大片 | 91午夜在线| 久久久精品一区 | 玖玖操 | 日韩久色 | 久久国产欧美一区二区三区精品 | www,99热| 91亚洲国产精品 | 欧美国产伦久久久久久 | 国产精品视频yy9299一区 | 成人免费视屏 | 日本久久精品视频 | 久久av资源 | 性高湖久久久久久久久 | www国产亚洲精品久久网站 | 精品亚洲成人 |