您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Springboot怎么根據(jù)實體類生成數(shù)據(jù)庫表”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
Springboot 實體類生成數(shù)據(jù)庫表
第一步:添加springboot-data-jpa和數(shù)據(jù)庫的依賴關(guān)系
第二步:編寫yml文件的配置
第三步:實體類中使用的注解
第四步:啟動項目是否生成表格
第五步:啟動項目即可
springboot繼承JPA根據(jù)實體類生成數(shù)據(jù)庫中的表
1. pom中添加的依賴
2. application.yml中配置jpa配置
定義用戶實體類,通過注解映射成數(shù)據(jù)庫中的表
啟動springboot項目
JPA:springboot -jpa:數(shù)據(jù)庫的一系列的定義數(shù)據(jù)持久化的標(biāo)準的體系
學(xué)習(xí)的目的是:
利用springboot實現(xiàn)對數(shù)據(jù)庫的操作
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
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
有更加詳細介紹
參考網(wǎng)址:
//kemok4.com/article/222622.htm
實體類的寫法:
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; } }
完成~
首先搭建springboot框架。搭建完成之后:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--mysql-connection--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
server: port: 8080 spring: datasource: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/h6mall?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 hikari: minimum-idle: 5 idle-timeout: 180000 maximum-pool-size: 10 auto-commit: true pool-name: MyHikariCP connection-timeout: 30000 jpa: hibernate: ddl-auto: update show-sql: true
其中jpa下的jpa.hibernate.ddl-auto屬性值有如下:
ddl-auto:create
(每次運行該程序,沒有表格會新建表格,表內(nèi)有數(shù)據(jù)會清空)
ddl-auto:create-drop
(每次程序結(jié)束的時候會清空表)
ddl-auto:update
(每次運行程序,沒有表格會新建表格,表內(nèi)有數(shù)據(jù)不會清空,只會更新)
ddl-auto:validate
(運行程序會校驗數(shù)據(jù)與數(shù)據(jù)庫的字段類型是否相同,不同會報錯)
一般情況下選擇update,其他屬性值慎用!
import javax.persistence.*; @Entity @Table(name = "user") @Data public class User { @Id @GeneratedValue private Long id; //name屬性為表的字段名。length為字段的長度 @Column(length = 30, name = "userId") private String userId; @Column(name = "userName", length = 20, columnDefinition="varchar(100) COMMENT '用戶名'") private String userName; @Column(name = "phone", length = 20) private String phone; @Column(name = "password", length = 30) private String password; @Column(name = "userRealName", length = 20) private String userRealName; @Column(name = "address", length = 20) private String address; }
可看到控制臺上顯示了創(chuàng)建表中的
然后查看數(shù)據(jù)庫中是否生成了對應(yīng)的表:
“Springboot怎么根據(jù)實體類生成數(shù)據(jù)庫表”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。