jpa实体类生成mysql表及字段注解

代码:其中有表有:名称、索引,字段有:名称、类型(int,varchar, text),唯一, 长度,是否为空,注释,默认值

索引说明:如果字段有别名也就是有name属性,则columnList 中填写字段name中的值,若无name,则填写字段名即可,异常时会发现不会自动创建与更新数据库,而此时也不会有任何异常提示

@ApiModel(value = "用户信息")
@Entity
// Indexes是声明表里的索引,columnList是索引的列,同时可声明此索引列是否唯一,
@Table(name = "fls_user", indexes = {@Index(name = "userMobile", columnList = "userMobile", unique = true)})
public class User implements Serializable {

	private static final long serialVersionUID = 1092120369362179355L;

	@ApiModelProperty(value = "ID", hidden = true)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	@ApiModelProperty(value = "排序")
	@Column(name = "user_sort", columnDefinition = "int(11) comment '排序'")
	private Integer userSort;

	@ApiModelProperty(value = "用户电话")
	@Column(name = "user_mobile", unique = true, columnDefinition = "int(11) comment '手机'")
	private String userMobile;

	@ApiModelProperty(value = "用户密码")
	@JsonIgnore
	@Column(name = "user_password", columnDefinition = "varchar(50) comment '密码'")
	private String userPassword;

	@ApiModelProperty(value = "用户图片")
	@Column(name = "user_photo", columnDefinition = "varchar(255) comment '头像'")
	private String userPhoto;

	@ApiModelProperty(value = "真实姓名")
	@Column(name = "user_realname", nullable = false, columnDefinition = "varchar(50) comment '姓名'")
	private String userRealName;

	@ApiModelProperty(value = "启用状态")
	@Column(name = "user_on", nullable = false, columnDefinition = "int(1) default 0 comment '启用状态:0:启用,1:禁用,2,:已删除'")
	// 默认启用
	private Integer userOn = EnumUtils.UserEnum.NORMAL.getValue();

	@ApiModelProperty(value = "当前单位")
	@Column(name = "user_company", columnDefinition = "int(11) comment '当前单位'")
	private Integer userCompany;

	@ApiModelProperty(value = "当前角色")
	@Column(name = "user_role", columnDefinition = "int(11) comment '当前角色'")
	private Integer userRole;

	@ApiModelProperty(value = "扩展角色")
	@Lob
	@Basic(fetch = FetchType.LAZY)
	@Column(name = "user_extend_role", columnDefinition = "text comment '扩展角色'")
	private String userExtendRole;

生成表截图如下

jpa实体类生成mysql表及字段注解_第1张图片

说明:有时候会出现以下异常,出现后当创建完成自己的索引后,删除掉自动创建的索引即可

当索引unique为true时,第二次启动时会创建名称为user_mobile的索引,此时删掉自动创建的索引即可

当索引unique为false时,若不需要则手动删除自动创建的索引

jpa实体类生成mysql表及字段注解_第2张图片

 

你可能感兴趣的:(jpa实体类生成mysql表及字段注解)