MySQL创建自动更新列

时间:2020-07-05 15:35:51   收藏:0   阅读:83

项目中很多时候需要自动更新的列表项,例如创建时间(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;
}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!