我有一个域类说在grails中获取最后两条记录的有效方法?
class Content{
String content
Date dateCreated
Date lastUpdated
}
有数据库中的一些内容记录。我想检索最新的两条记录。在Grails中查询两条最新记录的有效方法是什么?谢谢!
我有一个域类说在grails中获取最后两条记录的有效方法?
class Content{
String content
Date dateCreated
Date lastUpdated
}
有数据库中的一些内容记录。我想检索最新的两条记录。在Grails中查询两条最新记录的有效方法是什么?谢谢!
是不是
Content.list(max: 2, sort: "dateCreated", order: "desc")
的可能性?
你可以写一个HQL
查询作为 -
String queryString = "select content from Content order by lastUpdated desc"
List contentList = Content.executeQuery(queryString,[max: 2, offset: 0])
不管的 “武器” 你做出的选择...
HQL查询
def query = "SELECT content from Content order by lastUpdated desc"
def data = Content.executeQuery(query,[max: 2, offset: 0])
SQL查询
SELECT * FROM table_name ORDER BY lastUpdated DESC LIMIT 2
的Grails个createCriteria:
def c = Content.createCriteria()
def results = c.list {
order("lastUpdated", "desc")
maxResults(2)
}
...最佳选择的主要原则保持不变 - 顺序递减,并得到2个结果。
如果要检索的数据如此微不足道,则不需要实际的SQL或HQL查询。您可以使用Grails功能,它不会在性能上留下任何痕迹。
我不会主张任何这些方法。为什么不是'where'查询? –
你在使用Hibernate吗? –