本篇文章为大家展示了JPA如何设置默认字段及其长度,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
使用jpa去生成对应的值的长度和默认值是如何设置的呢
@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;
}
String
string是最常见的字段,
@Column(name = “name”)
private String name;
SQL中 name varchar(255)
生成的字段长度为255,即如果不设置长度的话默认的长度就是255个。
@Column(name = “name”,length = 50)
private String name;
name varchar(50)

@Column(name = “name”,columnDefinition=“varchar(11) COMMENT ‘用户姓名'”)
private String name;
name varchar(11) COMMENT ‘用户姓名'

这里不仅指定了长度,还给列了一个注释,便于查看
@Column(name = “name”,columnDefinition=“default ‘12345'”)
private String name;
如果我们制定默认值,这样SQL语句就会报错
create table user (id bigint not null, age integer, birth datetime(6), name default ‘12345', sex bit, primary key (id)) engine=InnoDB
如果我们制定长度呢
@Column(name = “name”,columnDefinition=“default ‘12345'”,length = 25)
private String name;
运行的DDL语句依然无法创建出表
create table user (id bigint not null, age integer, birth datetime(6), name default ‘12345', sex bit, primary key (id)) engine=InnoDB
如何给String字段指定默认值呢
所以如果指定columnDefinition这个属性会覆盖原来的列注解的长度,而且在其注解的中必须制定列的类型
/**
* (Optional) The SQL fragment that is used when
* generating the DDL for the column.
* <p> Defaults to the generated SQL to create a
* column of the inferred type.
*/
String columnDefinition() default "";
/**
*(可选)在以下情况下使用的SQL片段:
*正在为列生成DDL。
*默认使用生成的SQL来创建推断类型的列。
*/
@Column(name = “name”,columnDefinition=" varchar(11) default ‘12345'",length = 25)
private String name;
create table user (id bigint not null, age integer, birth datetime(6), name varchar(11) default ‘12345', sex bit, primary key (id)) engine=InnoDB
columnDefinition 会将其中的值作为列名之后,如果在这里设置默认值,必须保证直接加在列名之后执行不会出错。

定义 Integer 和 Long 有区别吗


Long的默认长度为20,Integer的默认长度为11
定义Integer和int有区别吗

区别是int如果你不给他设置就会默认为0
boolean和Boolean也是一样的。
boolean类型
默认长度为1
@Column(name = “sex”)
private Boolean sex;

@Column(name = “sex”)
private boolean sex;

二者没有什么区别,如果我们将boolean设置默认值并且设置长度会咋样呢?
@Column(name = “sex”,length = 50)
private boolean sex;
生成的DDL语句中并没有长度的属性。所以在boolean类型中设置长度属性是没有作用的
create table user (id bigint not null, age integer, birth datetime(6), name varchar(255), sex bit, primary key (id)) engine=InnoDB
指定boolean类型的默认值呢
@Column(name = “sex”,columnDefinition = “bit(1) default 1”)
private boolean sex;

有注意,如果指定默认值为1,即是true,属性如果不指定,boolean的默认值就是false,所以会将false设置到对应的数据中,如果指定Boolean呢
如果指定Boolean的话,没有设置默认就是null,就会将空更新到数据库中,最好用的方法还是在java类中设置默认值。
日期
默认的日期格式不具有可读性
@Column(name = "birth")
private Date birth;

使用@Temporal来制定格式
@Temporal(TemporalType.DATE)
@Column(name = "birth")
private Date birth;

@Temporal有三个值
DATE 只有日期
TIME 只有时间 时分秒
TIMESTAMP 日期和时间都有
浮点数格式的设置
@Column(name = “age”,scale = 2)
private int age;
age integer,属性为scale = 2没有作用
@Column(name = “age”,scale = 2)
private float age;

@Column(name = “age”,precision = 5,scale = 2)
private float age;
还是没有用,指定数据格式为double,依然没有用
@Column(name = “age”,columnDefinition = “decimal(5,2)”)
private double age;

大文本
/**
* 备注
*/
@Basic(fetch = FetchType.LAZY)
@Type(type = "text")
@Lob
@Column(name = "remark")
private String remark;
@Lob 指定该文本为长文本
@Basic(fetch = FetchType.LAZY) 长文本缓加载,便于数据的读取。
上述内容就是JPA如何设置默认字段及其长度,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注天达云行业资讯频道。