java 删除一年多以前的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35550019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete records from more than 1 year ago
提问by julien
I'm saving twitter tweets in my database with spring JPA Repositories. The date of the tweet is saved as Datetime in the MySQL db. Now I want to delete all tweets that are older than one year. I saw there is the function CURRENT_TIME
and I thought of something like CURRENT_TIME - 360
. I know thats not the correct syntax but I have no idea how to do this. Here is what I have:
我正在使用 spring JPA 存储库将 twitter 推文保存在我的数据库中。推文的日期在 MySQL 数据库中保存为 Datetime。现在我想删除所有超过一年的推文。我看到有这个功能CURRENT_TIME
,我想到了类似的东西CURRENT_TIME - 360
。我知道那不是正确的语法,但我不知道如何做到这一点。这是我所拥有的:
@Modifying
@Transactional
@Query("DELETE FROM Tweetpost t WHERE t.createdAt > ")
int removeOlderThan();
EDIT SOLVED:
编辑解决:
Repository:
存储库:
@Modifying
@Transactional
@Query("DELETE FROM Tweetpost m WHERE m.createdAt < :date")
int removeOlderThan(@Param("date") java.sql.Date date);
Service:
服务:
public void removeOldItems() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -360);
java.sql.Date oneYear = new java.sql.Date(cal.getTimeInMillis());
tweetRepository.removeOlderThan(oneYear);
}
回答by Alexius DIAKOGIANNIS
For this you need 2 steps. First of all you need a method that will take as a parameter the date of which you want to delete the messages and you dont need tha @Query
annotation at all.
为此,您需要 2 个步骤。首先,您需要一个方法,它将您想要删除消息的日期作为参数,而您@Query
根本不需要注解。
So in your repository you must have something like
所以在你的存储库中,你必须有类似的东西
@Modifying
public void deleteByCreatedAtBefore(Date expiryDate);
Now in your service method, you will calculate the Date and pass it on like this
现在在您的服务方法中,您将计算日期并像这样传递它
public void performTweetCleanup(){
//calculate date
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
cal.add(Calendar.YEAR, -1);
Date previousYear = cal.getTime();
//call the method
MyTweeterRepository.deleteByCreatedAtBefore(previousYear);
}
回答by julien
SOLVED:
解决了:
Repository:
存储库:
@Modifying
@Transactional
@Query("DELETE FROM Tweetpost m WHERE m.createdAt < :date")
int removeOlderThan(@Param("date") java.sql.Date date);
Service:
服务:
public void removeOldItems() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -360);
java.sql.Date oneYear = new java.sql.Date(cal.getTimeInMillis());
tweetRepository.removeOlderThan(oneYear);
}
回答by JB Nizet
Compute the current time minus one year in Java, and then use the follwoing query:
在 Java 中计算当前时间减去一年,然后使用以下查询:
DELETE FROM Tweetpost t WHERE t.createdAt < :nowMinusOneYear
回答by Alexander Heim
If you want the current time you could use
如果你想要当前时间,你可以使用
System.getCurrentTimeMillis()
Take the time from the old date and subtract it from the current one then you just have to compare it with the duration of one year. Maybe you should add something for leap-years.
从旧日期中取出时间并将其从当前日期中减去,然后您只需将其与一年的持续时间进行比较。也许你应该为闰年添加一些东西。