亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring Boot 2.5.0 重新設計的spring.sql.init 配置有啥用

瀏覽:56日期:2023-07-14 10:54:46
棄用內容

先來糾正一個誤區(qū)。主要之前在版本更新介紹的時候,存在一些表述上的問題。導致部分讀者認為這次的更新是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. */@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;

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

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

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

新的設計

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

下面我們通過一個簡單的例子來體驗這個功能的作用。

創(chuàng)建一個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>

在配置文件中增加數(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ù)上面配置的定義,接下來就在resource目錄下,創(chuàng)建腳本文件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 ’注冊地址:省’, cityvarchar(255) default ’’ null comment ’注冊地址:城市’, country varchar(255) default ’’ null comment ’注冊地址:縣/區(qū)’, status tinyint unsigned default 0 not null comment ’是否標記刪除 0:否 1:是’, create_time datetime not null comment ’創(chuàng)建時間’, update_time datetime not null comment ’更新時間’)comment ’用戶表’;

完成上面步驟之后,啟動應用。然后打開MySQL客戶端,可以看到在test庫下,多了一個user_info表

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

配置詳解

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

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

關于這些配置的應用,相信聰明的你一定會把它與數(shù)據(jù)庫的版本管理聯(lián)系起來(因為可以自動的執(zhí)行腳本)。

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

個人認為就上述所介紹的配置,雖然具備了一定的自動執(zhí)行能力。但由于缺失對當前環(huán)境的判斷能力,所以要應對實際的部署場景來說,還是遠遠不夠的。

如果要自動化的管理數(shù)據(jù)庫表結構、初始化數(shù)據(jù)的話,我的建議是:

默認提供的這個初始化功能可以且僅用于單元測試,自動創(chuàng)建數(shù)據(jù)庫結構與初始化數(shù)據(jù),使用完畢后銷毀。可以方便的控制每次單元測試的執(zhí)行環(huán)境一致。 應用在環(huán)境部署的時候,還是要使用之前介紹過的Flyway來實現(xiàn),如何使用可見之前的分享:使用Flyway來管理數(shù)據(jù)庫版本。 聯(lián)合Flyway一同使用,通過org.springframework.jdbc.datasource.init.DataSourceInitializer來定義更復雜的執(zhí)行邏輯。

更多本系列免費教程連載「點擊進入?yún)R總目錄」

代碼示例

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

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

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

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 国内自拍 在线播放 网红 | 萌白酱粉嫩福利视频在线观看 | 国产一级一片免费播放 | 在线精品自拍 | 亚洲国产91| 成人毛片在线播放 | 国产一区二区在线观看视频 | 一级做a爱 一区 | 美国人妖欧美性xxxxk妖 | 国产日韩精品一区二区 | 青草久久精品亚洲综合专区 | 加勒比一本一道在线 | 国产在线每日更新 | 亚洲夜夜骑 | 国产高清视频 | 欧美成人禁片在线观看网址 | 欧美特黄a级高清免费看片 欧美特黄a级猛片a级 | 国产在线精品一区二区夜色 | 91精品国产色综合久久不卡蜜 | 日本ab在线| 国产免费观看网站黄页 | 美国黄色在线观看 | 亚洲成人黄色网址 | 看一级片| 深夜爽爽爽gif福利免费 | 美女一区二区在线观看 | 一级特黄特交牲大片 | 丁香婷婷综合五月综合色啪 | 黑人好太好长爱不了 | 生活一级毛片 | 一级做a爰片久久毛片16 | 亚洲黑人巨大videos0 | 视频一本大道香蕉久在线播放 | 国产成a人片在线观看视频 国产成a人片在线观看视频99 | 国产高清在线91福利 | 一级视频在线免费观看 | 亚久久伊人精品青青草原2020 | 亚洲图片一区 | 在线观看高清视频bbixx | 欧美亚洲香蕉 | 99riav国产|