SpringBoot+jpa配置如何根據(jù)實體類自動創(chuàng)建表
jpa配置根據(jù)實體類自動創(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.編寫實體類
import javax.persistence.*;
import java.io.Serializable;
import java.util.Objects;
@Entity
//聲明實體類
public class User implements Serializable {
@Id
//聲明了實體唯一標識對應的屬性
@GeneratedValue
//自增
private Integer id;
@Column(nullable = false, unique = true, length = 32)
//長度32,唯一索引,nullable表示true可以為空,false不可以
//用來聲明實體屬性的表字段的定義
private String userName;
private String passWord;
private String email;
private String nickName;
private String regTime;
@Transient
//不映射成列的字段
private String desc;
//省略get和set方法
}
4.運行項目
啟動即可生成
5.針對項目啟動以后數(shù)據(jù)庫并未生成數(shù)據(jù)庫表問題
包導的不對: import javax.persistence.*;
配置文件不對: spring.jpa.hibernate.ddl-auto=update
注解寫的不對:不要忘記@Entity
…
還可能有一種原因:
Sprint的入口文件在子目錄里了,應該比其他諸如service、dao、controller、entity高一級。
例如:service文件所在為com.demo.metaService,那么入口文件xxxApplication.java應該在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 之類的錯誤
<!-- <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
個人建議創(chuàng)建一個基礎Entity,用于表中常用字段創(chuàng)建配合 mybatisplus,jackson,SnowFlake,lombok 等庫,自行導入相關注解請自行了解
BaseEntry.java
@Data//省略setget方法
@MappedSuperclass //標注父類
@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 = "唯一標識")
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)建時間")
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 = "更新時間")
private Date updateTime;
@ApiModelProperty(value = "刪除標志 默認0")
@TableLogic
private Integer delFlag = CommonConstant.STATUS_NORMAL;
}
業(yè)務Entry ,僅做參考
/**
* tb_bussiness_up_record實體類
*
* @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 = "經銷商")
private String bussinessId;
@ApiModelProperty(value = "審核時間")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private String auditTime;
}
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Springboot集合前端實現(xiàn)進度條顯示功能實例
這篇文章主要介紹了使用進度條提升用戶體驗的原因,特別是在處理大文件上傳、下載或長時間運行的操作時,進度條通過實時反饋任務進度,減少用戶的不確定感,文中給出了詳細的代碼示例,需要的朋友可以參考下2024-11-11
利用synchronized實現(xiàn)線程同步的案例講解
這篇文章主要介紹了利用synchronized實現(xiàn)線程同步的案例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02

