IntelliJ IDEA 2017版 spring-boot2.0.2 搭建 JPA springboot DataSource JPA 实体类浅谈

时间:2023-03-09 03:29:14
IntelliJ IDEA 2017版 spring-boot2.0.2 搭建 JPA springboot DataSource JPA 实体类浅谈

一、实体类分析

一般用到的实体类的类型有

String类型、Long类型、Integer类型、Double类型、Date类型、DateTime类型、Text类型、Boolean型等

1、String类型的写法

  @Column(nullable = false, length = 50)
public String mainName;

可以给字段设置两种权限,private和public两种,使用private时候要使用getter and setter,而使用public的时候不需要使用。

String类型作为普通字段的设置,可以采用@Column注解,属性可以填充name(字段名称)、length(长度)、nullable(字段是否为空,false不为空)、columnDefinition(拼接的ddl,就是sql语句)

加上这些字段的public完整设置(这些事比较常用的)

  @Column(nullable = false, length = 50,name = "mainName",columnDefinition = "COMMENT '名称'")
public String mainName;

注意:

当String作为主键的时候,设置如下:

 @Id
@GenericGenerator(strategy = "uuid", name = "mainId")
@GeneratedValue(generator = "mainId")
@Column(length = 40)
public String mainId;

2、Long类型

一般用法跟String基本一样

 @Column(name = "id",nullable = false,columnDefinition = "DEFAULT '1'",length = 50)
public Long id;

作为主键用法:

 @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long id;

3、Integer类型

 @Column(columnDefinition = "int(1) DEFAULT '1'",nullable = false,name = "deleted")
public Integer deleted;

作为主键:

 @Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Integer id;

4、double类型

一般情况:

  @Column(name = "priceCoefficient", columnDefinition = "decimal(50,4) default 1.0000 COMMENT '价格系数'",nullable = false)
public double priceCoefficient;

5、date类型

一般情况:

  @Temporal(TemporalType.DATE)
public Date trueTime;

6、DateTime类型

  @Temporal(TemporalType.TIMESTAMP)
public Date createTime;

7、Text类型

     @Lob
@Basic(fetch=FetchType.LAZY)
private String words;

8、Boolean型

 @org.hibernate.annotations.Type(type="yes_no")
private boolean manageLog = false;