好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于Column注解的columnDefinition用法

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

查看更多关于基于Column注解的columnDefinition用法的详细内容...

  阅读:24次