zoukankan      html  css  js  c++  java
  • Spring Jpa 自动建表——表字段设置默认值

    表字段设置默认值

    • 记录意义:学习记录
    • 方法一:使用@Column注解中的columnDefinition属性设置默认值
      • 代码如下
    @Entity
    @Table(name = "m_test")
    @DynamicInsert
    @DynamicUpdate
    public class TestTable implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id", columnDefinition = "BIGINT")
        private Long id;
    
        @Column(name = "user_name", columnDefinition = "varchar(50) NOT NULL COMMENT '用户名称'")
        private String userName;
    
        @Column(name = "address", columnDefinition = "varchar(50) NOT NULL COMMENT '用户地址'")
        private String address;
        
        @Column(name = "sex", columnDefinition = "tinyint NOT NULL DEFAULT 0 COMMENT '性别:0-男  1-女'")
        private Integer sex;
    
    }
      • 注意:在使用这个方法设置字段默认值时,如果定义了NOT NULL,则要在使用类注解@DynamicInsert和@DynamicUpdate,否则报错
    • 方法二:直接为类属性设置值
      • 代码如下
    @Entity
    @Table(name = "m_test")
    public class TestTable implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id", columnDefinition = "BIGINT")
        private Long id;
    
        @Column(name = "user_name", columnDefinition = "varchar(50) NOT NULL COMMENT '用户名称'")
        private String userName;
        
        @Column(name = "address", columnDefinition = "varchar(50) NOT NULL COMMENT '用户地址'")
        private String address;
        
        @Column(name = "sex", columnDefinition = "tinyint NOT NULL'")
        private Integer sex = 0;
    
    }
    • 建议:使用方法二

    时间类型

    • 记录意义:在数据库设置时间类型时,有的使用timestamp ,有的使用datetime,个人推荐使用datetime
    • 代码如下
    @Entity
    @Table(name = "m_test")
    public class TestTable implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id", columnDefinition = "BIGINT")
        private Long id;
    
        @Column(name = "user_name", columnDefinition = "varchar(50) NOT NULL COMMENT '用户名称'")
        private String userName;
        
        @Column(name = "address", columnDefinition = "varchar(50) NOT NULL COMMENT '用户地址'")
        private String address;
        
        @Column(name = "sex", columnDefinition = "tinyint NOT NULL'")
        private Integer sex = 0;
    
        @JsonFormat
        @Column(name = "cdate", columnDefinition = "DATETIME DEFAULT CURRENT_TIMESTAMP ")
        private LocalDateTime cdate = LocalDateTime.now();
    
        @JsonFormat
        @Column(name = "mdate", columnDefinition = "DATETIME DEFAULT CURRENT_TIMESTAMP ")
        private LocalDateTime mdate = LocalDateTime.now();
    
    }
  • 相关阅读:
    [Bada开发]基于bada1.0的5种控件介绍[待续]
    [Bada开发]API官方学习2-风格
    [Bada开发]HelloWorld篇
    [Bada开发]初步入口函数介绍
    [Bada开发]使用共享库
    [Bada开发]使用静态库
    [Bada开发]OpenGL ES 2.0程序 创建简单3D图形
    [Bada开发]播放实时rtsp流
    剑指offer-删除链表中重复的结点
    剑指offer-构建乘积数组
  • 原文地址:https://www.cnblogs.com/zuiyue_jing/p/15411185.html
Copyright © 2011-2022 走看看