<noframes id="bhrfl"><address id="bhrfl"></address>

    <address id="bhrfl"></address>

    <noframes id="bhrfl"><address id="bhrfl"><th id="bhrfl"></th></address>

    <form id="bhrfl"><th id="bhrfl"><progress id="bhrfl"></progress></th></form>

    <em id="bhrfl"><span id="bhrfl"></span></em>

    全部
    常見問題
    產品動態
    精選推薦

    Spring Boot 2.x基礎教程:2.5版本后數據腳本初始化的變動

    管理 管理 編輯 刪除

    Spring Boot 2.5.0版本發布后,其中提到了關于Datasource初始化機制的調整,那這方面到底做了什么調整?那么今天就詳細說說這個重新設計的配置內容,并結合實際情況聊一聊相關理解和實踐建議。

    棄用內容

    先來糾正一個誤區。主要之前在版本更新介紹的時候,存在一些表述上的問題。有些朋友認為這次的更新是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.
    	 */
    	@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.usernamespring.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`;所以,上面的例子第二次執行的時候會報錯并啟動失敗,因為第一次執行的時候表已經存在。

    應用建議

    關于這些配置的應用,相信有些朋友一定會把它與數據庫的版本管理聯系起來(因為可以自動的執行腳本)。

    那么依靠這些配置,是否可以勝任業務應用部署時候數據庫初始化的自動化實現呢?

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

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

    1. 默認提供的這個初始化功能可以且僅用于單元測試,自動創建數據庫結構與初始化數據,使用完畢后銷毀??梢苑奖愕目刂泼看螁卧獪y試的執行環境一致。
    2. 應用在環境部署的時候,還是要使用之前介紹過的Flyway來實現,如何使用可見之前的分享:使用Flyway來管理數據庫版本。
    3. 聯合Flyway一同使用,通過org.springframework.jdbc.datasource.init.DataSourceInitializer來定義更復雜的執行邏輯。


    注:本文轉載自“程序猿DD”,如有侵權,請聯系刪除!

    請登錄后查看

    哈哈哈醬 最后編輯于2024-12-25 17:09:01

    快捷回復
    回復
    回復
    回復({{post_count}}) {{!is_user ? '我的回復' :'全部回復'}}
    排序 默認正序 回復倒序 點贊倒序

    {{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

    作者 管理員 企業

    {{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
    {{item.is_suggest == 1? '取消推薦': '推薦'}}
    沙發 板凳 地板 {{item.floor}}#
    {{item.user_info.title || '暫無簡介'}}
    附件

    {{itemf.name}}

    {{item.created_at}}  {{item.ip_address}}
    打賞
    已打賞¥{{item.reward_price}}
    {{item.like_count}}
    {{item.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復

    {{itemc.user_info.nickname}}

    {{itemc.user_name}}

    回復 {{itemc.comment_user_info.nickname}}

    附件

    {{itemf.name}}

    {{itemc.created_at}}
    打賞
    已打賞¥{{itemc.reward_price}}
    {{itemc.like_count}}
    {{itemc.showReply ? '取消回復' : '回復'}}
    刪除
    回復
    回復
    查看更多
    打賞
    已打賞¥{{reward_price}}
    1244
    {{like_count}}
    {{collect_count}}
    添加回復 ({{post_count}})

    相關推薦

    快速安全登錄

    使用微信掃碼登錄
    {{item.label}} 加精
    {{item.label}} {{item.label}} 板塊推薦 常見問題 產品動態 精選推薦 首頁頭條 首頁動態 首頁推薦
    取 消 確 定
    回復
    回復
    問題:
    問題自動獲取的帖子內容,不準確時需要手動修改. [獲取答案]
    答案:
    提交
    bug 需求 取 消 確 定
    打賞金額
    當前余額:¥{{rewardUserInfo.reward_price}}
    {{item.price}}元
    請輸入 0.1-{{reward_max_price}} 范圍內的數值
    打賞成功
    ¥{{price}}
    完成 確認打賞

    微信登錄/注冊

    切換手機號登錄

    {{ bind_phone ? '綁定手機' : '手機登錄'}}

    {{codeText}}
    切換微信登錄/注冊
    暫不綁定
    亚洲欧美字幕
    CRMEB客服

    CRMEB咨詢熱線 咨詢熱線

    400-8888-794

    微信掃碼咨詢

    CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
    返回頂部 返回頂部
    CRMEB客服