javascript
jpa获取数据库当前时间_SpringDataJPA存储数据时通过注解自动设置创建时间和修改时间...
如果我們此時是通過 Spring Data JPA進行數據庫的操作,Spring Data JAP提供了Auditing特性,我們可以通過起很好的實現我們的需求。
其原因基本時通過插入監聽器,當我們對被特定注解的數據bean進行操作時,其在中間自動進行一系列的操作,想到個詞,補刀~~
我們看下官方的說明:
Spring Data provides sophisticated support to transparently keep track of who created or changed an entity and the point in time this happened. To benefit from that functionality you have to equip your entity classes with auditing metadata that can be defined either using annotations or by implementing an interface.
大意如下:Spring Data 提供了復雜的支持來用透明的,無感知的方式去跟蹤實體是被誰創建的以及發生此時間的時間點。
基于注解的auditing元數據
@CreatedBy,
@LastModifiedBy :捕獲實體是被誰創建的或者修改
@CreatedDate :捕獲創建的時間
@LastModifiedDate :捕獲修改的時間
使用時,我們的數據bean需要添加@EntityListeners(AuditingEntityListener.class)
注解,一邊增加auditing的能力,當然,我們的配置應用類還要添加@EnableJpaAuditing
注解去開啟auditing特性。
例如:
@Entity
@EntityListeners(AuditingEntityListener.class)
class Customer {
@CreatedBy
private User user;
@CreatedDate
private DateTime createdDate;
// … further properties omitted
}
@SpringBootApplication
@EnableJpaAuditing
public class AircraftApplication {
public static void main(String[] args) {
SpringApplication.run(AircraftApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
總結
以上是生活随笔為你收集整理的jpa获取数据库当前时间_SpringDataJPA存储数据时通过注解自动设置创建时间和修改时间...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑怎么做表格初学者如何在电脑上制作表格
- 下一篇: extjs5的grid垂直滚动条bug_