基于@LastModifiedDate不起作用的解決方案
@LastModifiedDate不起作用
在實(shí)體中添加注解 @EntityListeners(AuditingEntityListener.class)監(jiān)聽實(shí)體變化
在自動更新時間戳字段增加 @LastModifiedDate
在Spring boot啟動類增加注解 @EnableJpaAuditing啟用JPA審計(jì)(自動填充默認(rèn)值)
如果你是使用JPA的save(實(shí)體)方法去更新數(shù)據(jù)是沒有問題的,如果是使用SQL/JPQL語句就會失效。
比如:
@Query("update xxx set x = ? where x = ?")
這里提供最簡單的解決辦法,語句里時間字段賦值CURRENT_TIMESTAMP即可。
JPA中@CreatedDate和@LastModifiedDate的使用
前些時間寫了新項(xiàng)目,然后嘗試使用了Spring Data JPA,發(fā)現(xiàn)新世界。很多功能都可以基于注解實(shí)現(xiàn),為開發(fā)省去了不少功夫。
關(guān)于時間的生成注解@CreatedDate和@LastModifiedDate的使用,在此記錄一下。
使用步驟
1.在實(shí)體類上加上注解 @EntityListeners(AuditingEntityListener.class),在相應(yīng)的字段上添加對應(yīng)的時間注解 @LastModifiedDate 和 @CreatedDate。
注意:日期的類型可以使用Date,也可以使用Long。我一般習(xí)慣用Date。
//@Data lombok注解,替我們生成getter和setter。 @Data @Entity @Table(name = "task") @EntityListeners(AuditingEntityListener.class) public class Task { ? ? /** ? ? ?* 自增主鍵 ? ? ?*/ ? ? @Id ? ? @GeneratedValue(strategy = GenerationType.IDENTITY) ? ? @Column(name = "id") ? ? private Integer id; ? ? /** ? ? ?* 創(chuàng)建時間 ? ? ?*/ ? ? @CreatedDate ? ? @Column(name = "createTime", columnDefinition = "timestamp not null default current_timestamp") ? ? private Date createTime; ? ? /** ? ? ?* 更新時間 ? ? ?*/ ? ? @LastModifiedDate ? ? @Column(name = "updateTime", columnDefinition = "timestamp not null default current_timestamp") ? ? private Date updateTime; }
2.在Application啟動類中添加注解 @EnableJpaAuditing。
@EnableJpaAuditing @SpringBootApplication public class TestApplication { ? ? public static void main(String[] args) { ? ? ? ? SpringApplication.run(TestApplication.class, args); ? ? }? }
3.除了上面提到的注解外,Spring Data JPA 還提供 @CreatedBy 和 @LastModifiedBy 注解,用于保存和更新當(dāng)前操作用戶的信息(如id、name)。
如果有這方面的需求,可以參考下面的配置實(shí)現(xiàn)
代碼如下:
@Data @Entity @EntityListeners(AuditingEntityListener.class) public class Task { ? ?? ?/** ? ? ?* 自增主鍵 ? ? ?*/ ? ? @Id ? ? @GeneratedValue(strategy = GenerationType.IDENTITY) ? ? @Column(name = "id") ? ? private Integer id; ? ? /** ? ? ?* 創(chuàng)建時間 ? ? ?*/ ? ? @CreatedDate ? ? @Column(name = "createTime", columnDefinition = "timestamp not null default current_timestamp") ? ? private Date createTime; ? ? /** ? ? ?* 更新時間 ? ? ?*/ ? ? @LastModifiedDate ? ? @Column(name = "updateTime", columnDefinition = "timestamp not null default current_timestamp") ? ? private Date updateTime; ? ? ?/** ? ? ?* 創(chuàng)建人 ? ? ?*/ ? ? @CreatedBy ? ? @Column(name = "createBy", columnDefinition = "varchar(255) not null") ? ? private String createBy; ? ? /** ? ? ?* 最后修改人 ? ? ?*/ ? ? @LastModifiedBy ? ? @Column(name = "lastModifiedBy", columnDefinition = "varchar(255) not null") ? ? private String lastModifiedBy; }
獲取操作員信息
/** ?* Spring Data JPA通過AuditorAware<T>接口獲取用戶信息, ?* 其中泛型T可以為String保存用戶名,也可以為Long/Integer保存用戶ID。 ?* @author EvanWang ?* ?*/ @Component public class AuditorConfig implements AuditorAware<String> { ? ? /** ? ? ?* 返回操作員標(biāo)志信息 ? ? ?* ? ? ?* @return ? ? ?*/ ? ? @Override ? ? public Optional<String> getCurrentAuditor() { ? ? ? ? // 這里應(yīng)根據(jù)實(shí)際業(yè)務(wù)情況獲取具體信息 ? ? ? ? return Optional.of(userName); ? ? } }
補(bǔ)充注解
Hibernate 也提供了類似上述時間注解的功能實(shí)現(xiàn),這種方法只需要一步配置,更改為注解 @UpdateTimestamp 和 @CreationTimestamp
代碼如下:
@Data @MappedSuperclass @NoArgsConstructor @AllArgsConstructor public class Task { ? ? @Id ? ? @GeneratedValue(strategy = GenerationType.IDENTITY) ? ? @Column(name = "id") ? ? private Integer id; ? ? @UpdateTimestamp ? ? @Column(name = "updateTime", columnDefinition = "timestamp not null default current_timestamp") ? ? private Date updateTime; ? ? @CreationTimestamp ? ? @Column(name = "updateTime", columnDefinition = "timestamp not null default current_timestamp") ? ? private Date createTime; }
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
idea使用war以及war exploded的區(qū)別說明
本文詳細(xì)解析了war與warexploded兩種部署方式的差異及步驟,war方式是先打包成war包,再部署到服務(wù)器上;warexploded方式是直接把文件夾、class文件等移到Tomcat上部署,支持熱部署,開發(fā)時常用,文章分別列出了warexploded模式和war包形式的具體操作步驟2024-10-10學(xué)會IDEA REST Client后就可以丟掉postman了
這篇文章主要介紹了學(xué)會IDEA REST Client后就可以丟掉postman了,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Java使用POI導(dǎo)出Excel(二):多個sheet
這篇文章介紹了Java使用POI導(dǎo)出Excel的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10MyBatis-Plus中MetaObjectHandler沒生效完美解決
在進(jìn)行測試時發(fā)現(xiàn)配置的MyMetaObjectHandler并沒有生效,本文主要介紹了MyBatis-Plus中MetaObjectHandler沒生效完美解決,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11SpringBoot項(xiàng)目的五種創(chuàng)建方式
這篇文章主要介紹了SpringBoot項(xiàng)目的五種創(chuàng)建方式,文中通過圖文結(jié)合的方式講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-12-12