SpringBootJpa根據(jù)Entry自動生成表

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;
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容