溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

利用Jpa怎么更新表中的創(chuàng)建日期和修改時(shí)間

發(fā)布時(shí)間:2021-01-30 14:07:30 來源:億速云 閱讀:385 作者:Leah 欄目:開發(fā)技術(shù)

利用Jpa怎么更新表中的創(chuàng)建日期和修改時(shí)間?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

在阿里Java開發(fā)手冊(cè)中也對(duì)此的說明:

【強(qiáng)制】表必備三字段:id, create_time, update_time。

說明:其中 id 必為主鍵,類型為 bigint unsigned、單表時(shí)自增、步長為 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 來開啟時(shí)間的支持, 在字段上使用 @CreatedDate 和@LastModifiedDate 注解來即可完成時(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ù)庫映射實(shí)體類時(shí),通常需要記錄createTime和updateTime,如果每個(gè)對(duì)象新增或修改去都去手工操作創(chuàng)建時(shí)間、更新時(shí)間,會(huì)顯得比較繁瑣。

Springboot jpa提供了自動(dòng)填充這兩個(gè)字段的功能,簡單配置一下即可。@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注解。

做完這些,我們來測(cè)試一下,新建個(gè)Springboot項(xiàng)目,配置一下數(shù)據(jù)庫信息

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ì)更改的。

關(guān)于利用Jpa怎么更新表中的創(chuàng)建日期和修改時(shí)間問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

jpa
AI