Column注解的columnDefinition使用
columnDefinition屬性表示創建表時,該字段創建的SQL語句,一般用于通過Entity生成表定義時使用,如果數據庫中表已經建好,該屬性沒有必要使用
1、指定字段類型、長度、是否允許null、是否唯一、默認值
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
:定義了被標注字段在數據庫表中所對應字段的名稱; -
unique
:表示該字段是否為唯一標識,默認為false。如果表中有一個字段需要唯一標識,則既可以使用該標記,也可以使用@Table標記中的 -
nullable
:表示該字段是否可以為null值,默認為true -
insertable
:表示在使用“INSERT”腳本插入數據時,是否需要插入該字段的值。 -
updatable
:表示在使用“UPDATE”腳本插入數據時,是否需要更新該字段的值。insertable和updatable屬性一般多用于只讀的屬性,例如主鍵和外鍵等。這些字段的值通常是自動生成的。 -
columnDefinition
(大多數情況,幾乎不用):表示創建表時,該字段創建的SQL語句,一般用于通過Entity生成表定義時使用。(也就是說,如果DB中表已經建好,該屬性沒有必要使用。 -
table
:表示當映射多個表時,指定表的表中的字段。默認值為主表的表名。 -
length
:表示字段的長度,當字段的類型為varchar時,該屬性才有效,默認為255個字符。 -
precision
和scale
:precision屬性和scale屬性表示精度,當字段類型為double時,precision表示數值的總長度,scale表示小數點所占的位數
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/WZH577/article/details/97933549