[英]Automatically delete row from database after 6 months
我希望我的程序從我的數據庫中刪除舊故事。 當故事 6 個月大時,它應該被自動刪除。
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private long id;
private String title;
@Column(columnDefinition = "TEXT")
private String content;
private Date posted;
我什至不知道如何開始編程。 我讀過它很慢,我需要某種事件,但我不知道該怎么做。 如果你能幫我解決這個問題,我會非常高興。
您可以使用@Scheduled
在特定時刻運行方法並檢查是否有超過 6 個月的數據。
例如
@Component
public class Janior {
@Scheduled(cron = "0 0 0 * * ?") // this will run daily
public void deleteOldStories() {
// delete all entries older than 6 month
}
}
確保將@EnableScheduling
添加到 SpringBootApplication class。
閱讀有關調度的更多信息: https://spring.io/guides/gs/scheduling-tasks/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.