如何使用Grails的Gorm删除日期超过两周前的所有行



我是Gorm的新手,目前有一项要求,需要删除所有超过两周的记录。

现在我正在这样查询我的表:

String query = "select a from history a where successful = :successful"
List<History> histories = History.executeQuery(query, null, [max:null, offset:null])
for (History history: histories){
Date date1 = New Date()
Date date2 = New Date(history.date)
use(groovy.time.TimeCategory) {
def duration = date1 - date2
if (duration.days > 14){
// delete here
}
}
}

我确信有一种更好、更有效的方法可以使用gorm删除所有超过14天的记录,我想知道是否有人知道如何做到这一点?非常感谢。

GORM文档推荐了几个选项,比如:

def twoWeeksAgo = use(TimeCategory){ new Date() - 2.weeks }
History.where{
date < twoWeeksAgo
}.deleteAll()

最新更新