亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Springboot啟動(dòng)同時(shí)創(chuàng)建數(shù)據(jù)庫和表實(shí)現(xiàn)方法

 更新時(shí)間:2023年01月14日 11:10:13   作者:風(fēng).foxwho  
這篇文章主要介紹了Springboot啟動(dòng)同時(shí)創(chuàng)建數(shù)據(jù)庫和表,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

自動(dòng)創(chuàng)建數(shù)據(jù)庫

spring boot 自帶 如果數(shù)據(jù)庫不存在,可以自動(dòng)創(chuàng)建數(shù)據(jù)庫

spring.datasource.url=jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true&characterEncoding=utf8mb4&useSSL=false&allowPublicKeyRetrieval=true

createDatabaseIfNotExist=true 數(shù)據(jù)庫連接加此參數(shù)即可,但是數(shù)據(jù)庫名稱,中間不可以有 - 字符(橫線 或 減號(hào)),但 下劃線可以

自動(dòng)創(chuàng)建表

引用包

    api('org.springframework.boot:spring-boot-starter-web:2.6.2')
    api('org.springframework.boot:spring-boot-starter-data-jpa:2.6.2')
    implementation('mysql:mysql-connector-java:8.0.27')

使用 JPA 設(shè)置庫表對(duì)應(yīng)實(shí)體

/**
 * 設(shè)備相關(guān)的所有日志
 */
@Data
@Entity
@Table(name = "test")
public class Test
     /**
    * id
     **/
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
    /**
    * 名稱
     **/
	private String name;
}

在啟動(dòng)時(shí)會(huì)自動(dòng) 創(chuàng)建 表,如果表已經(jīng)存在,但新增加的字段不存在那么,會(huì)自動(dòng)創(chuàng)建字段,如果字段已經(jīng)存在,那么什么也不改變。

所以,如果實(shí)體對(duì)應(yīng)的字段 類型變了,需要手動(dòng)去更改字段類型

此處受spring.jpa.hibernate.ddl-auto配置影響

自動(dòng)執(zhí)行初始化sql 文件

配置如下

spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

ddl-auto 枚舉:
none(默認(rèn)):禁用DDL處理
validate:驗(yàn)證schema,不做任何操作
update: 更新schema
create: 刪除表,重新創(chuàng)建schema
create-drop: 會(huì)話創(chuàng)建時(shí)創(chuàng)建schema,會(huì)話關(guān)閉時(shí)銷毀schema

初始化時(shí),如果 resources目錄下存在 schema.sql文件和data.sql文件,那么會(huì)自動(dòng)執(zhí)行。

如果文件不存在則不執(zhí)行。

使用最多時(shí)data.sql文件,自動(dòng)生成一些定義好的數(shù)據(jù)

到此這篇關(guān)于Springboot啟動(dòng)同時(shí)創(chuàng)建數(shù)據(jù)庫和表實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Springboot創(chuàng)建數(shù)據(jù)庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論