MySQL创建自动更新列

项目中很多时候需要自动更新的列表项,例如创建时间(createTime)和修改时间(updateTime),所以的在建表的时候需要特殊处理一下:

create table `account` (
    `id` int primary key auto_increment,
    `title` varchar(32) not null comment ‘标题‘,
    `price` decimal(6,2) default 0.01 comment ‘价格‘,
    `username` varchar(16) not null comment ‘账号‘,
    `password` varchar(16) not null comment ‘密码‘,
    `create_time` timestamp default current_timestamp comment ‘创建时间‘,
    `update_time` timestamp default current_timestamp on update current_timestamp comment ‘修改时间‘
) comment ‘账号表‘;

然后修改对应的实体类,添加@DynamicInsert和@DynamicUpdate注解

@Data
@Entity
@DynamicInsert
@DynamicUpdate
public class Account {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String title;
    private BigDecimal price;
    private String username;
    private String password;
    private Date createTime;
    private Date updateTime;
}

相关推荐