SpringBoot+jpa配置如何根據(jù)實(shí)體類自動創(chuàng)建表
jpa配置根據(jù)實(shí)體類自動創(chuàng)建表
1.配置文件application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/bootTable?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.database=mysql spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
2.pom.xml引入包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
3.編寫實(shí)體類
import javax.persistence.*; import java.io.Serializable; import java.util.Objects; @Entity //聲明實(shí)體類 public class User implements Serializable { @Id //聲明了實(shí)體唯一標(biāo)識對應(yīng)的屬性 @GeneratedValue //自增 private Integer id; @Column(nullable = false, unique = true, length = 32) //長度32,唯一索引,nullable表示true可以為空,false不可以 //用來聲明實(shí)體屬性的表字段的定義 private String userName; private String passWord; private String email; private String nickName; private String regTime; @Transient //不映射成列的字段 private String desc; //省略get和set方法 }
4.運(yùn)行項(xiàng)目
啟動即可生成
5.針對項(xiàng)目啟動以后數(shù)據(jù)庫并未生成數(shù)據(jù)庫表問題
包導(dǎo)的不對: import javax.persistence.*;
配置文件不對: spring.jpa.hibernate.ddl-auto=update
注解寫的不對:不要忘記@Entity
…
還可能有一種原因:
Sprint的入口文件在子目錄里了,應(yīng)該比其他諸如service、dao、controller、entity高一級。
例如:service文件所在為com.demo.metaService,那么入口文件xxxApplication.java應(yīng)該在com.demo下
jpa根據(jù)Entry自動生成表
1.加入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
若有依賴 spring-data-jpa 則刪掉,否則會出現(xiàn)找不到 bootstrap 之類的錯(cuò)誤
<!-- <dependency>--> <!-- <groupId>org.springframework.data</groupId>--> <!-- <artifactId>spring-data-jpa</artifactId>--> <!-- <version>2.1.4.RELEASE</version>--> <!-- </dependency>-->
2.配置 application.yml
增加Jpa 自動生成表的配置
spring: jpa: ##配置自動建表:updata:沒有表新建,有表更新操作,控制臺顯示建表語句 hibernate: ddl-auto: update show-sql: true
3. 創(chuàng)建Entity
個(gè)人建議創(chuàng)建一個(gè)基礎(chǔ)Entity,用于表中常用字段創(chuàng)建配合 mybatisplus,jackson,SnowFlake,lombok 等庫,自行導(dǎo)入相關(guān)注解請自行了解
BaseEntry.java
@Data//省略setget方法 @MappedSuperclass //標(biāo)注父類 @EntityListeners(AuditingEntityListener.class) //jpa數(shù)據(jù)監(jiān)聽 @JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"}) //忽略解析的字段 public abstract class BaseEntry implements Serializable{ private static final long serialVersionUID = 1L; @Id @TableId @ApiModelProperty(value = "唯一標(biāo)識") private String id = String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId()); @ApiModelProperty(value = "創(chuàng)建者") @CreatedBy private String createBy; @CreatedDate @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "創(chuàng)建時(shí)間") private Date createTime; @ApiModelProperty(value = "更新者") @LastModifiedBy private String updateBy; @LastModifiedDate @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "更新時(shí)間") private Date updateTime; @ApiModelProperty(value = "刪除標(biāo)志 默認(rèn)0") @TableLogic private Integer delFlag = CommonConstant.STATUS_NORMAL; }
業(yè)務(wù)Entry ,僅做參考
/** * tb_bussiness_up_record實(shí)體類 * * @author * */ @Data @Entity @Table(name = "tb_bussiness_up_record") @TableName("tb_bussiness_up_record") public class TbBussinessUpRecord extends BaseEntry { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "經(jīng)銷商") private String bussinessId; @ApiModelProperty(value = "審核時(shí)間") @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private String auditTime; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java對象中什么時(shí)候適合用static修飾符踩坑解決記錄
這篇文章主要為大家介紹了java對象中什么時(shí)候適合用static修飾符踩坑解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09SpringBoot中的@Configuration注解詳解
這篇文章主要介紹了SpringBoot中的@Configuration注解詳解,Spring Boot推薦使用JAVA配置來完全代替XML 配置,JAVA配置就是通過 @Configuration和 @Bean兩個(gè)注解實(shí)現(xiàn)的,需要的朋友可以參考下2023-08-08使用redis的increment()方法實(shí)現(xiàn)計(jì)數(shù)器功能案例
這篇文章主要介紹了使用redis的increment()方法實(shí)現(xiàn)計(jì)數(shù)器功能案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11Spring Boot集成Java DSL的實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring Boot集成Java DSL的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01idea批量啟動多個(gè)微服務(wù)具體實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于idea批量啟動多個(gè)微服務(wù)的具體實(shí)現(xiàn),在微服務(wù)開發(fā)過程中,我們經(jīng)常要在本地啟動很多個(gè)微服務(wù),文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07