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

一、实体类分析

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

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

 1、String类型的写法

1  @Column(nullable = false, length = 50)
2     public String mainName;
View Code

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

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

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

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

注意:

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

1 @Id
2     @GenericGenerator(strategy = "uuid", name = "mainId")
3     @GeneratedValue(generator = "mainId")
4     @Column(length = 40)
5     public String mainId;
View Code

2、Long类型

一般用法跟String基本一样

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

作为主键用法:

1 @Id
2     @GeneratedValue(strategy=GenerationType.IDENTITY)
3     public Long id;
View Code

3、Integer类型

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

作为主键:

1 @Id
2     @GeneratedValue(strategy=GenerationType.IDENTITY)
3     public Integer id;
View Code

4、double类型

一般情况:

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

5、date类型

一般情况:

1  @Temporal(TemporalType.DATE)
2     public Date trueTime;
View Code

6、DateTime类型

1  @Temporal(TemporalType.TIMESTAMP)
2     public Date createTime;
View Code

7、Text类型

1     @Lob  
2     @Basic(fetch=FetchType.LAZY)  
3     private String words;
View Code

8、Boolean型

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

猜你喜欢

转载自www.cnblogs.com/liuyangfirst/p/9202709.html