https://blog.csdn.net/u010783936/article/details/793958989(原文)
import?com.fasterxml.jackson.annotation.JsonIgnoreProperties;??
import?org.hibernate.validator.constraints.NotBlank;??
import?org.springframework.data.annotation.CreatedDate;??
import?org.springframework.data.annotation.LastModifiedDate;??
import?org.springframework.data.jpa.domain.support.AuditingEntityListener;??
import?javax.persistence.*;??
import?java.util.Date;??
/**
?*
?*/??
@Entity??
@Table(name?=?"notes")??
@EntityListeners(AuditingEntityListener.class)??
@JsonIgnoreProperties(value?=?{"createdAt",?"updatedAt"},??
allowGetters?=true)??
public?class?Note?{??
@Id??
@GeneratedValue(strategy?=?GenerationType.AUTO)??
private?Long?id;??
@NotBlank??
private?String?title;??
@NotBlank??
private?String?content;??
@Column(nullable?=?false,?updatable?=?false)??
@Temporal(TemporalType.TIMESTAMP)??
@CreatedDate??
private?Date?createdAt;??
@Column(nullable?=?false)??
@Temporal(TemporalType.TIMESTAMP)??
@LastModifiedDate??
private?Date?updatedAt;??
public?Long?getId()?{??
return?id;??
????}??
public?void?setId(Long?id)?{??
this.id?=?id;??
????}??
public?String?getTitle()?{??
return?title;??
????}??
public?void?setTitle(String?title)?{??
this.title?=?title;??
????}??
public?String?getContent()?{??
return?content;??
????}??
public?void?setContent(String?content)?{??
this.content?=?content;??
????}??
public?Date?getCreatedAt()?{??
return?createdAt;??
????}??
public?void?setCreatedAt(Date?createdAt)?{??
this.createdAt?=?createdAt;??
????}??
public?Date?getUpdatedAt()?{??
return?updatedAt;??
????}??
public?void?setUpdatedAt(Date?updatedAt)?{??
this.updatedAt?=?updatedAt;??
????}??
}??
如上示例代碼,
[java]?view plain?copy
@EntityListeners(AuditingEntityListener.class)?是用于監(jiān)聽實(shí)體類添加或者刪除操作的耻涛。??
[java]?view plain?copy
@JsonIgnoreProperties(value?=?{"createdAt",?"updatedAt"},??
allowGetters?=true)??這個(gè)注解是用于在除了在獲取createAt,updateAt屬性時(shí)進(jìn)行操作废酷,其他創(chuàng)建和更新操作都由jpa完成??
[java]?view plain?copy
@Column(nullable?=?false,?updatable?=?false)?其中updatable?=?false表示不進(jìn)行更新操作??
[java]?view plain?copy
@CreatedDate?表示該字為創(chuàng)建時(shí)間字段,在這個(gè)實(shí)體被insert時(shí)抹缕,設(shè)置值澈蟆;@LastModifiedDate同理??