SpringBoot+jpa配置如何根據實體類自動創建表

jpa配置根據實體類自動創建表

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.針對項目啟動以後數據庫並未生成數據庫表問題

包導的不對: import javax.persistence.*;

配置文件不對: spring.jpa.hibernate.ddl-auto=update

註解寫的不對:不要忘記@Entity

還可能有一種原因:

Sprint的入口文件在子目錄裡瞭,應該比其他諸如service、dao、controller、entity高一級。

例如:service文件所在為com.demo.metaService,那麼入口文件xxxApplication.java應該在com.demo下

jpa根據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;
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: