Jpa 實(shí)現(xiàn)自動(dòng)更新表中的創(chuàng)建日期和修改時(shí)間
一般來(lái)說創(chuàng)建時(shí)間和修改時(shí)間 兩個(gè)字段是一個(gè)實(shí)體類必備的。
在阿里Java開發(fā)手冊(cè)中也對(duì)此的說明:
【強(qiáng)制】表必備三字段:id, create_time, update_time。
說明:其中 id 必為主鍵,類型為 bigint unsigned、單表時(shí)自增、步長(zhǎng)為 1。create_time, update_time 的類型均為 datetime 類型,前者現(xiàn)在時(shí)表示主動(dòng)式創(chuàng)建,后者過去分詞表示被動(dòng)式更新。
mysql 實(shí)現(xiàn)添加時(shí)間自動(dòng)添加更新時(shí)間自動(dòng)更新
在JPA 中也是支持新的數(shù)據(jù)保存是自動(dòng)寫入創(chuàng)建時(shí)間,當(dāng)數(shù)據(jù)有修改時(shí) 自動(dòng)記錄修改時(shí)間。在SpringBoot 的啟動(dòng)類上加 @EnableJpaAuditing 來(lái)開啟時(shí)間的支持, 在字段上使用 @CreatedDate 和@LastModifiedDate 注解來(lái)即可完成時(shí)間的自動(dòng)更新。
實(shí)例:
@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; ... }
由于這兩個(gè)字段所有實(shí)體類都有,所以可以將它們抽取到一個(gè)通用的類里面,其他實(shí)體類需要時(shí)直接繼承即可。
/** * 所有類的超類 * 自動(dòng)更新創(chuàng)建時(shí)間和更新時(shí)間 * * @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 { .... }
補(bǔ)充:Jpa配置實(shí)體類創(chuàng)建時(shí)間更新時(shí)間自動(dòng)賦值,@CreateDate,@LastModifiedDate
操作數(shù)據(jù)庫(kù)映射實(shí)體類時(shí),通常需要記錄createTime和updateTime,如果每個(gè)對(duì)象新增或修改去都去手工操作創(chuàng)建時(shí)間、更新時(shí)間,會(huì)顯得比較繁瑣。
Springboot jpa提供了自動(dòng)填充這兩個(gè)字段的功能,簡(jiǎn)單配置一下即可。@CreatedDate、@LastModifiedDate、@CreatedBy、@LastModifiedBy前兩個(gè)注解就是起這個(gè)作用的,后兩個(gè)是設(shè)置修改人和創(chuàng)建人的,這里先不討論。
首先,我們的很多實(shí)體類都是需要?jiǎng)?chuàng)建時(shí)間和更新時(shí)間的,我們不想在每個(gè)實(shí)體類里都去定義這兩個(gè)字段,那么我們把它抽取到基類中,讓實(shí)體類去繼承它。
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標(biāo)簽開啟后,下面的時(shí)間標(biāo)簽才會(huì)生效。
然后還需要在啟動(dòng)類加上@EnableJpaAuditing注解。
做完這些,我們來(lái)測(cè)試一下,新建個(gè)Springboot項(xiàng)目,配置一下數(shù)據(jù)庫(kù)信息
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:
新建個(gè)普通的實(shí)體類。
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; } }
測(cè)試類:
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); // } }
先試試新增。
可以看到已經(jīng)被自動(dòng)賦值了。
然后試試update,將上面的update的注釋放開。
可以看到更新時(shí)間也自動(dòng)修改了。
需注意,如果你沒有修改任何字段的值的話,即便走了save方法,updateTime也是不會(huì)更改的。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Spring實(shí)戰(zhàn)之Qualifier注解用法示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之Qualifier注解用法,結(jié)合實(shí)例形式詳細(xì)分析了spring Qualifier注解相關(guān)配置、定義與使用方法,需要的朋友可以參考下2019-12-12升級(jí)springboot中spring框架的版本的實(shí)現(xiàn)方法
本文主要介紹了升級(jí)springboot中spring框架的版本的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08SpringBoot2.3.0配置JPA的實(shí)現(xiàn)示例
這篇文章主要介紹了SpringBoot2.3.0配置JPA的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java 中的CharArrayReader 介紹_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
CharArrayReader 是字符數(shù)組輸入流。它和ByteArrayInputStream類似,只不過ByteArrayInputStream是字節(jié)數(shù)組輸入流,而CharArray是字符數(shù)組輸入流。CharArrayReader 是用于讀取字符數(shù)組,它繼承于Reader2017-05-05java synchronized同步靜態(tài)方法和同步非靜態(tài)方法的異同
這篇文章主要介紹了java synchronized同步靜態(tài)方法和同步非靜態(tài)方法的異同的相關(guān)資料,需要的朋友可以參考下2017-01-01