@Column注解

[@Column]
 用于指定列的相关属性
    name - 可选,表示数据库表中列的名称。
            默认为属性或字段名称。
    nullable - 可选,表示该字段是否允许为 null,默认为 true(null)
            若设置为false 则该列不可为null值
    unique - 可选,表示该字段是否是唯一标识,默认为 false(不唯一)
            若为true 则表示该列唯一
            如 uuid, email, mobile 等属性
    length - 可选,表示该字段的大小,仅对 String 类型的字段有效,默认值 255.
            用来自定义列的长度 如 mobile (length=11)
        insertable - 可选,表示在 ORM 框架执行插入操作时,该字段是否应出现 INSETRT
            语句中,默认为 true
        updateable - 可选,表示在 ORM 框架执行更新操作时,该字段是否应该出现在 UPDATE 语句中,
            默认为 true. 对于一经创建就不可以更改的字段,该属性非常有用,
            如对于 birthday 字段。
            或者创建时间/注册时间(可以将其设置为 false 不可修改)
        precision 可选,列十进制精度(decimal precision)(默认值 0)
        scale 可选,如果列十进制数值范围(decimal scale)可用,在此设置(默认值 0)
        columnDefinition - 可选,表示该字段在数据库中的实际类型。
    
 通常 ORM 框架可以根据属性类型自动判断数据库中字段的类型,
    但是对于 Date 类型仍无法确定数据库中字段类型究竟是 DATE,TIME 还是 TIMESTAMP. 此
    外 ,String 的默认映射类型为 VARCHAR, 如果要将 String 类型映射到特定数据库的 BLOB
    或 TEXT 字段类型,该属性非常有用。
    示例 :
    @Column(name="BIRTHDAY",nullable = false,columnDefinition="DATE")
    public String getBithday() {
        return birthday;
    }
    @Column(name = "id", nullable = false, columnDefinition = "BIGINT UNSIGNED")
    private Long id;
    @Column(updatable = false, name = "flight_name", nullable = false, length=50)
    public String getName() { ... }
    @Lob
    @Column(columnDefinition="text")
    public String content;

 

package sun.rain.amazing.javax.anno.domain;

import lombok.Data;

import javax.persistence.*;
import java.util.Date;

/**
 * @author  sunRainAmazing
 */
@Entity
@Data
public class UserColumn {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    /**
     * name uuid
     * length 12
     * not null
     * unique key
     */
    @Column(length = 12,nullable = false,unique = true)
    private String uuid;
    @Column(name="uname")
    private String username;
    /**
     * columnDefinition  自定义sql 片段
     *    类型 + 条件
     */
    @Column(columnDefinition = "char(11) not null")
    private String mobile;

    @Column(nullable = false,unique = true)
    private String email;

    @Column(updatable = false,columnDefinition="DATE")
    private Date createTime;

    @Column(updatable = false,columnDefinition="TIMESTAMP")
    private Date regTime;

    private Date updateTime;


    @Lob
    @Column(columnDefinition="text")
    public String content;

    @Lob
    public String descMsg;

    public UserColumn(String username, String email) {
        this.username = username;
        this.email = email;
    }

    public UserColumn() {
    }
}
/*

CREATE TABLE `user_column` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` text,
  `create_time` date DEFAULT NULL,
  `desc_msg` longtext,
  `email` varchar(255) NOT NULL,
  `mobile` char(11) NOT NULL,
  `reg_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `update_time` datetime DEFAULT NULL,
  `uname` varchar(255) DEFAULT NULL,
  `uuid` varchar(12) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `UK_31xsg913l27grh28ju8e8w70u` (`email`),
  UNIQUE KEY `UK_qnp9dji8vmb6irh0pjpx5llro` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

 */



--------------------- 
原文:https://blog.csdn.net/sunrainamazing/article/details/80783226 
 

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值