springboot根據(jù)實體類生成表的實現(xiàn)方法
JPA:springboot -jpa:數(shù)據(jù)庫的一系列的定義數(shù)據(jù)持久化的標準的體系
學(xué)習(xí)的目的是:
利用springboot實現(xiàn)對數(shù)據(jù)庫的操作
第一步:添加springboot-data-jpa和數(shù)據(jù)庫的依賴關(guān)系
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
第二步:編寫yml文件的配置;
server: port: 8001 spring: application: name: jih-manage datasource: name: test url: jdbc:mysql://111.231.231.56/jih username: root password: root type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver jpa: hibernate: ddl-auto: update show-sql: true
第三步:實體類中使用的注解:
@Entity 實體類的注解 @Id 映射到表格中id的屬性 @Gernertervalue 添加其自增的屬性;
第四步:啟動項目是否生成表格
補充的知識點:
根據(jù)實體類生成數(shù)據(jù)庫的表配置文件有倆種方式分別是yml和properties文件進行配置
yml文件:
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/facemap username: root password: root jpa: hibernate: ddl-auto: update show-sql: true
properties文件的寫法:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/dbgirl?characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.jpa.show-sql= true spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jackson.serialization.indent_output=false
有更加詳細介紹
首先我是通過maven創(chuàng)建了一個spring boot的工程,引入了Spring data jpa,結(jié)果實體類創(chuàng)建好之后,運行工程卻沒有在數(shù)據(jù)庫中自動創(chuàng)建數(shù)據(jù)表。
找了半天發(fā)現(xiàn)是一個配置的問題:
hibernate.ddl-auto節(jié)點的配置,這個配置有兩種方式去配置,我使用的是通過properties文件去配置:
#DataSource Config spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:6033/data_service?characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.jpa.show-sql= true spring.jpa.hibernate.ddl-auto=update spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jackson.serialization.indent_output=false
hibernate.hbm2ddl.auto節(jié)點的值有幾個create、create-drop、update、validate、none
- create:每次加載hibernate會自動創(chuàng)建表,以后啟動會覆蓋之前的表,所以這個值基本不用,嚴重會導(dǎo)致的數(shù)據(jù)的丟失。
- create-drop : 每次加載hibernate時根據(jù)model類生成表,但是sessionFactory一關(guān)閉,表就自動刪除,下一次啟動會重新創(chuàng)建。
- update:加載hibernate時根據(jù)實體類model創(chuàng)建數(shù)據(jù)庫表,這是表名的依據(jù)是@Entity注解的值或者@Table注解的值,sessionFactory關(guān)閉表不會刪除,且下一次啟動會根據(jù)實體model更新結(jié)構(gòu)或者有新的實體類會創(chuàng)建新的表。
- validate:啟動時驗證表的結(jié)構(gòu),不會創(chuàng)建表
- none:啟動時不做任何操作
實體類的寫法:
package com.example.demo; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @Entity //實體類的注解 public class Girl { @Id //@id注意選擇這個javax.persistence @GeneratedValue private Integer id; private String cupSize; private Integer age; public Girl() { } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCupSize() { return cupSize; } public void setCupSize(String cupSize) { this.cupSize = cupSize; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
第五步:啟動項目即可
到此這篇關(guān)于springboot根據(jù)實體類生成表的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)springboot 實體類生成表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatisPlus+Lombok實現(xiàn)分頁功能的方法詳解
Lombok是一個Java類庫,提供了一組注解,簡化POJO實體類開發(fā)。本文將為大家介紹一下Lombok的使用以及如何利用MyBatisPlus+Lombok實現(xiàn)分頁功能,感興趣的可以動手嘗試一下2022-07-07利用Springboot+vue實現(xiàn)圖片上傳至數(shù)據(jù)庫并顯示的全過程
最近遇到個需求,需要將圖片在前端上傳到服務(wù)器進行保存,然后讀取到前端進行展示,這篇文章主要給大家介紹了關(guān)于利用Springboot+vue實現(xiàn)圖片上傳至數(shù)據(jù)庫并顯示的相關(guān)資料,需要的朋友可以參考下2023-04-04Java四舍五入時保留指定小數(shù)位數(shù)的五種方式
這篇文章主要介紹了Java四舍五入時保留指定小數(shù)位數(shù)的五種方式,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-09-09Netty分布式Future與Promise執(zhí)行回調(diào)相關(guān)邏輯剖析
這篇文章主要為大家介紹了Netty分布式Future與Promise執(zhí)行回調(diào)相關(guān)邏輯剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03