springboot-jpa自動創(chuàng)建數(shù)據(jù)庫表

第一步創(chuàng)建springboot項目,jpa,mysql,
代碼如下:
application.yml

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/springboot_test
    username: root
    password: 514730
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    hibernate:
#     更新或者創(chuàng)建數(shù)據(jù)表結(jié)構(gòu)
      ddl-auto: update
#    控制臺顯示SQL
    show-sql: true

實體類

import javax.persistence.*;

@Entity
@Table(name = "person")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private  Long id;

    @Column(length =50,name = "name")
    private  String  name;

    private  String email;

    private  String  gender;

    private  String  address;
}

運行springboot主配置文件,來到數(shù)據(jù)庫界面管理軟件。發(fā)現(xiàn)我們的"person"表 自動創(chuàng)建好了


圖片.png

記得給主鍵ID加上:@GeneratedValue(strategy = GenerationType.IDENTITY)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容