热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Grails找到了排序、顺序、最大值和偏移量?-GrailsfindAllwithsort,order,maxandoffset?

Iwanttointegratesort,order,maxandoffsetinafindAllquery.Thefollowingworksfine:我想在fin

I want to integrate sort, order, max and offset in a findAll query. The following works fine:

我想在findAll查询中集成排序、顺序、最大值和偏移量。以下工作正常:

def books = Book.findAll("from Book as b where b.approved=true order by b.dateCreated  desc", [max: max, offset: offset])

But what I want is:

但我想要的是:

def books = Book.findAll("from Book as b where b.approved=true", [sort: 'dateCreated', order: 'desc', max: max, offset: offset])

This does not work. How do I have to rewrite this?

这并不工作。我怎么重写这个呢?

4 个解决方案

#1


7  

HQL doesn't support sort and order as parameters, so you need to include the "order by" as part of the HQL expression

HQL不支持sort和order作为参数,因此您需要将“order by”作为HQL表达式的一部分。

def books = Book.findAll("from Book as b where b.approved=true"
  + " order by b.dateCreated desc", [max: max, offset: offset])

(or in this case just use Book.findAllByApproved(true, [...]) instead of HQL).

(或者在本例中只使用Book。findAllByApproved(true,[…])而不是HQL。

So if the sort and order are variables you need a trick like

如果排序和顺序是变量,你需要一个技巧。

def books = Book.findAll("from Book as b where b.approved=true"
  + (params.sort ? " order by b.${params.sort} ${params.order}" : ''), 
  [max: max, offset: offset])

#2


6  

Using a where query works for me:

使用where查询为我工作:

def books = Book.where{approved == true}.list(sort: 'dateCreated', order: 'desc', max: max, offset: offset)

def书=书。{批准= = true }。列表(排序:'dateCreated', order: 'desc', max: max, offset: offset)

Or with params straight from the page:

或者直接从页面上看到:

def books = Book.where{approved == true}.list(params)

def书=书。在批准= = true } { .list(params)

#3


0  

Using "findAllBy" because it supports sort and order.

使用findAllBy,因为它支持排序和排序。

def results = Book.findAllByTitle("The Shining",
             [max: 10, sort: "title", order: "desc", offset: 100])

Click here for details.

请点击这里查看细节。

#4


0  

I am assuming you are calling fetching the list of books in a controller or a service class.

我假设您正在调用在控制器或服务类中获取图书列表。

If you are calling this from a controller action, then a magic variable "params" is already available to you. For example, if you request the page as follows

如果您是从控制器动作调用此操作,那么您已经可以使用一个神奇的变量“params”。例如,如果您请求页面如下。

book/list?max=10&offset=2

then "params" will already have those values mapped automagically.

然后“params”将会自动地映射这些值。

You can add more items to the params map as follows

您可以将更多项目添加到params映射中,如下所示。

params.sort = "dateCreated"
params.order = "desc"

Once you have build your params as desired, then you can use Grails dynamic query as follows

一旦您按照需要构建了您的params,那么您就可以使用Grails动态查询了。

def books = Book.findAllByApproved(true, params)
// use "books" variable as you wish

推荐阅读
author-avatar
努力的蚂蚁
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有