Column注解的columnDefinition使用
columnDefinition屬性表示創(chuàng)建表時,該字段創(chuàng)建的SQL語句,一般用于通過Entity生成表定義時使用,如果數(shù)據(jù)庫中表已經(jīng)建好,該屬性沒有必要使用
1、指定字段類型、長度、是否允許null、是否唯一、默認(rèn)值
1
2
3
|
/** 倉庫編號 */ @Column (name = "code" ,columnDefinition = "Varchar(100) not null default'' unique" ) private String code; |
2、需要特殊指定字段類型的情況
1
2
|
@Column (name = "remark" ,columnDefinition= "text" ) private String remark; |
1
2
|
@Column (name = "salary" , columnDefinition = "decimal(5,2)" ) private BigDecimal salary; |
1
2
3
4
|
@Column (name= "birthday" ,columnDefinition= "date" ) private Date birthday; @Column (name= "createTime" ,columnDefinition= "datetime" ) private Date createTime; |
@Column注解的各個字段的解釋
查看源碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Target ({ElementType.METHOD, ElementType.FIELD}) @Retention (RetentionPolicy.RUNTIME) public @interface Column { String name() default "" ; boolean unique() default false ; boolean nullable() default true ; boolean insertable() default true ; boolean updatable() default true ; String columnDefinition() default "" ; String table() default "" ; int length() default 255 ; int precision() default 0 ; int scale() default 0 ; } |
解釋
-
name
:定義了被標(biāo)注字段在數(shù)據(jù)庫表中所對應(yīng)字段的名稱; -
unique
:表示該字段是否為唯一標(biāo)識,默認(rèn)為false。如果表中有一個字段需要唯一標(biāo)識,則既可以使用該標(biāo)記,也可以使用@Table標(biāo)記中的 -
nullable
:表示該字段是否可以為null值,默認(rèn)為true -
insertable
:表示在使用“INSERT”腳本插入數(shù)據(jù)時,是否需要插入該字段的值。 -
updatable
:表示在使用“UPDATE”腳本插入數(shù)據(jù)時,是否需要更新該字段的值。insertable和updatable屬性一般多用于只讀的屬性,例如主鍵和外鍵等。這些字段的值通常是自動生成的。 -
columnDefinition
(大多數(shù)情況,幾乎不用):表示創(chuàng)建表時,該字段創(chuàng)建的SQL語句,一般用于通過Entity生成表定義時使用。(也就是說,如果DB中表已經(jīng)建好,該屬性沒有必要使用。 -
table
:表示當(dāng)映射多個表時,指定表的表中的字段。默認(rèn)值為主表的表名。 -
length
:表示字段的長度,當(dāng)字段的類型為varchar時,該屬性才有效,默認(rèn)為255個字符。 -
precision
和scale
:precision屬性和scale屬性表示精度,當(dāng)字段類型為double時,precision表示數(shù)值的總長度,scale表示小數(shù)點(diǎn)所占的位數(shù)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/WZH577/article/details/97933549