springboot jpa注解自动生成修改表,添加索引、字段注释、字段长度、引擎等

用jpa自动生成表,只用@Entity、@Table这两个注解生成的表:引擎是MyISAM、字段长度全是默认最大、字段没注释、没有添加索引

  • 先设置InnoDB引擎:走起

  • 生成表注解,并制定唯一索引(索引名、字段、类型):

    @Entity
    @Table(name = "pmi_t_user_version",
           indexes = {@Index(name = "my_index_name",  columnList="user_id", unique = true)}
       )
    
  • 添加表名注释:

     @org.hibernate.annotations.Table(appliesTo = "pmi_t_user_version",comment="用户登录版本信息")
    
  • 制定字段类型、长度、注释

      @ApiModelProperty(value = "用户id", required = true)
      @NotBlank(message = "用户id不能为空")
      @Column(name = "user_id", nullable = false,columnDefinition="int(11) COMMENT '用户id'")
      private long userId;
    
  • 完整代码:

     @Entity
      @Table(name = "pmi_t_user_version",
              indexes = {@Index(name = "my_index_name",  columnList="user_id", unique = true)}
          )
      @org.hibernate.annotations.Table(appliesTo = "pmi_t_user_version",comment="用户登录版本信息")
      @Component
      @ApiModel(value = "用户")
      public class UserVersion implements Serializable {
      
          /**
           * 主键id
           */
          @ApiModelProperty(value = "主键id")
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
          private long id;
          /**
           * 用户id
           */
      
          @ApiModelProperty(value = "用户id", required = true)
          @NotBlank(message = "用户id不能为空")
          @Column(name = "user_id", nullable = false,columnDefinition="int(11) COMMENT '用户id'")
          private long userId;
      
      
      
      
          /**
           * 版本号
           */
          @ApiModelProperty(value = "版本号", required = true)
          @NotBlank(message = "版本号不能为空")
          @Column(name = "version_no", nullable = false,columnDefinition="varchar(1) COMMENT '版本号'")
          private int versionNo;
          /**
           * 客户端类型
           */
          @ApiModelProperty(value = "客户端类型", required = true)
          @NotBlank(message = "客户端类型不能为空")
          @Column(name = "client_type", nullable = false,columnDefinition="varchar(1) COMMENT '客户端类型'")
          private int clientType;
      
          public long getId() {
              return id;
          }
      
          public void setId(long id) {
              this.id = id;
          }
      
          public long getUserId() {
              return userId;
          }
      
          public void setUserId(long userId) {
              this.userId = userId;
          }
      
          public int getVersionNo() {
              return versionNo;
          }
      
          public void setVersionNo(int versionNo) {
              this.versionNo = versionNo;
          }
      
          public int getClientType() {
              return clientType;
          }
      
          public void setClientType(int clientType) {
              this.clientType = clientType;
          }
      }
    
    
  • 表截图:
    springboot jpa注解自动生成修改表,添加索引、字段注释、字段长度、引擎等_第1张图片

  • 只有@Entity、@Table这里注解时,没有相应注解前截图
    springboot jpa注解自动生成修改表,添加索引、字段注释、字段长度、引擎等_第2张图片

你可能感兴趣的:(jpa)