springboot集成jpa自动创建更新时间
更多文章欢迎访问个人博客 www.herobin.top
使用springboot+jpa时,直接在dao层集成JpaRepository就可以直接使用接口内部定义好的增删改查方法了。
这里我们可以优化实体类与数据库之间的映射,让每次新建记录的时候自动产生create_time和update_time,每次更新的时候自动更新update_time。
数据库创建语句:
CREATE TABLE `product_category` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_name` varchar(64) NOT NULL COMMENT '类目名字',
`category_type` int(11) NOT NULL COMMENT '类目编号',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
对应表ProductCategory.java:
package com.imooc.entity;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
/**
* 类目
* Created by binzhang on 18/7/21.
*/
@Entity
//用这个注解才能实现动态更新(update_time的更新)
@DynamicUpdate
public class ProductCategory {
@Id
@GeneratedValue
private Integer categoryId;
// 类目名字
private String categoryName;
// 类目编号
private Integer categoryType;
private Date createTime;
private Date updateTime;
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Integer getCategoryType() {
return categoryType;
}
public void setCategoryType(Integer categoryType) {
this.categoryType = categoryType;
}
public Date getCreateTime() {return createTime;}
public void setCreateTime(Date createTime) {this.createTime = createTime;}
public Date getUpdateTime() {return updateTime;}
public void setUpdateTime(Date updateTime) {this.updateTime = updateTime;}
@Override
public String toString() {
return "ProductCategory{" +
"categoryId=" + categoryId +
", categoryName='" + categoryName + '\'' +
", categoryType=" + categoryType +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
}
这样更新的时候我们就可以不用去管create_time和update_time了。