获取grails中最后两条记录的有效方法?
我有一个域名类说获取grails中最后两条记录的有效方法?,grails,gorm,Grails,Gorm,我有一个域名类说 class Content{ String content Date dateCreated Date lastUpdated } 数据库中几乎没有内容记录。我想检索最近的两条记录。在Grails中查询两条最新记录的有效方法是什么?谢谢 不是 Content.list(max: 2, sort: "dateCreated", order: "desc") 一种可能性?您可以编写一个HQL查询,如下所示- String queryString = "selec
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])
SELECT * FROM table_name ORDER BY lastUpdated DESC LIMIT 2
SQL查询def query = "SELECT content from Content order by lastUpdated desc"
def data = Content.executeQuery(query,[max: 2, offset: 0])
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功能,它不会在性能上留下任何痕迹。您使用的是Hibernate吗?我不提倡任何这些方法。为什么不进行
where
查询?