SpringBootJpa根据Entry自动生成表

SpringBootJpa根据Entry自动生成表

1.加入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

若有依赖 spring-data-jpa 则删掉,否则会出现找不到 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. 创建Entity

个人建议创建一个基础Entity,用于表中常用字段创建
配合 mybatisplus,jackson,SnowFlake,lombok 等库,自行导入
相关注解请自行了解

BaseEntry.java

@Data//省略setget方法
@MappedSuperclass //标注父类
@EntityListeners(AuditingEntityListener.class) //jpa数据监听
@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 = "创建者")
    @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 = "创建时间")
    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;
}

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

转载于:https://www.jianshu.com/p/0a31ec81de77

猜你喜欢

转载自blog.csdn.net/weixin_33670786/article/details/91058381