SpringBoot+jpa配置如何根據(jù)實(shí)體類自動(dòng)創(chuàng)建表
jpa配置根據(jù)實(shí)體類自動(dòng)創(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)識(shí)對(duì)應(yīng)的屬性
@GeneratedValue
//自增
private Integer id;
@Column(nullable = false, unique = true, length = 32)
//長(zhǎng)度32,唯一索引,nullable表示true可以為空,false不可以
//用來(lái)聲明實(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)目
啟動(dòng)即可生成
5.針對(duì)項(xiàng)目啟動(dòng)以后數(shù)據(jù)庫(kù)并未生成數(shù)據(jù)庫(kù)表問(wèn)題
包導(dǎo)的不對(duì): import javax.persistence.*;
配置文件不對(duì): spring.jpa.hibernate.ddl-auto=update
注解寫的不對(duì):不要忘記@Entity
…
還可能有一種原因:
Sprint的入口文件在子目錄里了,應(yīng)該比其他諸如service、dao、controller、entity高一級(jí)。
例如:service文件所在為com.demo.metaService,那么入口文件xxxApplication.java應(yīng)該在com.demo下
jpa根據(jù)Entry自動(dòng)生成表
1.加入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
若有依賴 spring-data-jpa 則刪掉,否則會(huì)出現(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 自動(dòng)生成表的配置
spring:
jpa: ##配置自動(dòng)建表:updata:沒(méi)有表新建,有表更新操作,控制臺(tái)顯示建表語(yǔ)句
hibernate:
ddl-auto: update
show-sql: true
3. 創(chuàng)建Entity
個(gè)人建議創(chuàng)建一個(gè)基礎(chǔ)Entity,用于表中常用字段創(chuàng)建配合 mybatisplus,jackson,SnowFlake,lombok 等庫(kù),自行導(dǎo)入相關(guān)注解請(qǐng)自行了解
BaseEntry.java
@Data//省略setget方法
@MappedSuperclass //標(biāo)注父類
@EntityListeners(AuditingEntityListener.class) //jpa數(shù)據(jù)監(jiān)聽(tīng)
@JsonIgnoreProperties(value={"hibernateLazyInitializer","handler","fieldHandler"}) //忽略解析的字段
public abstract class BaseEntry implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@TableId
@ApiModelProperty(value = "唯一標(biāo)識(shí)")
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對(duì)象中什么時(shí)候適合用static修飾符踩坑解決記錄
這篇文章主要為大家介紹了java對(duì)象中什么時(shí)候適合用static修飾符踩坑解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
SpringBoot中的@Configuration注解詳解
這篇文章主要介紹了SpringBoot中的@Configuration注解詳解,Spring Boot推薦使用JAVA配置來(lái)完全代替XML 配置,JAVA配置就是通過(guò) @Configuration和 @Bean兩個(gè)注解實(shí)現(xiàn)的,需要的朋友可以參考下2023-08-08
使用redis的increment()方法實(shí)現(xiàn)計(jì)數(shù)器功能案例
這篇文章主要介紹了使用redis的increment()方法實(shí)現(xiàn)計(jì)數(shù)器功能案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Spring Boot集成Java DSL的實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring Boot集成Java DSL的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
idea批量啟動(dòng)多個(gè)微服務(wù)具體實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于idea批量啟動(dòng)多個(gè)微服務(wù)的具體實(shí)現(xiàn),在微服務(wù)開(kāi)發(fā)過(guò)程中,我們經(jīng)常要在本地啟動(dòng)很多個(gè)微服務(wù),文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07

