SpringBootJpa根據(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)建一個基礎(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)建時間")
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 = "刪除標(biāo)志 默認(rèn)0")
@TableLogic
private Integer delFlag = CommonConstant.STATUS_NORMAL;
}
業(yè)務(wù)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 = "經(jīng)銷商")
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;
}