Jpa 實現自動更新表中的創(chuàng)建日期和修改時間
一般來說創(chuàng)建時間和修改時間 兩個字段是一個實體類必備的。
在阿里Java開發(fā)手冊中也對此的說明:
【強制】表必備三字段:id, create_time, update_time。
說明:其中 id 必為主鍵,類型為 bigint unsigned、單表時自增、步長為 1。create_time, update_time 的類型均為 datetime 類型,前者現在時表示主動式創(chuàng)建,后者過去分詞表示被動式更新。
在JPA 中也是支持新的數據保存是自動寫入創(chuàng)建時間,當數據有修改時 自動記錄修改時間。在SpringBoot 的啟動類上加 @EnableJpaAuditing 來開啟時間的支持, 在字段上使用 @CreatedDate 和@LastModifiedDate 注解來即可完成時間的自動更新。
實例:
@EnableJpaAuditing
@SpringBootApplication
public class StudentApplication {
public static void main(String[] args) {
SpringApplication.run(StudentApplication.class, args);
}
}
@EntityListeners(value = AuditingEntityListener.class)
@Getter
@Setter
@Entity
public class StudentEntity {
....
@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createTime;
@LastModifiedDate
@Column()
private LocalDateTime updateTime;
...
}
由于這兩個字段所有實體類都有,所以可以將它們抽取到一個通用的類里面,其他實體類需要時直接繼承即可。
/**
* 所有類的超類
* 自動更新創(chuàng)建時間和更新時間
*
* @author peter
*
**/
@MappedSuperclass
@EntityListeners(value = AuditingEntityListener.class)
@Getter
@Setter
public abstract class AbstractBaseTimeEntity {
@CreatedDate
@Column(nullable = false, updatable = false)
private LocalDateTime createTime;
@LastModifiedDate
@Column()
private LocalDateTime updateTime;
}
@Entity
@Data
public class StudentEntity extends AbstractBaseTimeEntity {
....
}
補充:Jpa配置實體類創(chuàng)建時間更新時間自動賦值,@CreateDate,@LastModifiedDate
操作數據庫映射實體類時,通常需要記錄createTime和updateTime,如果每個對象新增或修改去都去手工操作創(chuàng)建時間、更新時間,會顯得比較繁瑣。
Springboot jpa提供了自動填充這兩個字段的功能,簡單配置一下即可。@CreatedDate、@LastModifiedDate、@CreatedBy、@LastModifiedBy前兩個注解就是起這個作用的,后兩個是設置修改人和創(chuàng)建人的,這里先不討論。
首先,我們的很多實體類都是需要創(chuàng)建時間和更新時間的,我們不想在每個實體類里都去定義這兩個字段,那么我們把它抽取到基類中,讓實體類去繼承它。
package com.tianyalei.testautotime.entity;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
/**
* Created by wuwf on 17/4/21.
*/
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Integer id;
@CreatedDate
private Long createTime;
@LastModifiedDate
private Long updateTime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Long getCreateTime() {
return createTime;
}
public void setCreateTime(Long createTime) {
this.createTime = createTime;
}
public Long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
}
AuditingEntityListener標簽開啟后,下面的時間標簽才會生效。
然后還需要在啟動類加上@EnableJpaAuditing注解。
做完這些,我們來測試一下,新建個Springboot項目,配置一下數據庫信息
spring: jpa: database: mysql show-sql: true hibernate: ddl-auto: update datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test username: root password:
新建個普通的實體類。
package com.tianyalei.testautotime.entity;
import javax.persistence.Entity;
@Entity
public class Post extends BaseEntity {
private String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
測試類:
import com.tianyalei.testautotime.entity.Post;
import com.tianyalei.testautotime.repository.PostRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestautotimeApplicationTests {
@Autowired
PostRepository postRepository;
@Test
public void save() {
Post post = new Post();
post.setTitle("title0");
postRepository.save(post);
}
// @Test
// public void update() {
// Post post = postRepository.findOne(1);
// post.setTitle(“title1”);
// postRepository.save(post);
// }
}
先試試新增。
可以看到已經被自動賦值了。
然后試試update,將上面的update的注釋放開。
可以看到更新時間也自動修改了。
需注意,如果你沒有修改任何字段的值的話,即便走了save方法,updateTime也是不會更改的。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
Spring實戰(zhàn)之Qualifier注解用法示例
這篇文章主要介紹了Spring實戰(zhàn)之Qualifier注解用法,結合實例形式詳細分析了spring Qualifier注解相關配置、定義與使用方法,需要的朋友可以參考下2019-12-12
Java 中的CharArrayReader 介紹_動力節(jié)點Java學院整理
CharArrayReader 是字符數組輸入流。它和ByteArrayInputStream類似,只不過ByteArrayInputStream是字節(jié)數組輸入流,而CharArray是字符數組輸入流。CharArrayReader 是用于讀取字符數組,它繼承于Reader2017-05-05
java synchronized同步靜態(tài)方法和同步非靜態(tài)方法的異同
這篇文章主要介紹了java synchronized同步靜態(tài)方法和同步非靜態(tài)方法的異同的相關資料,需要的朋友可以參考下2017-01-01

