热门标签 | 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

推荐阅读
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 本文详细介绍 Go+ 编程语言中的上下文处理机制,涵盖其基本概念、关键方法及应用场景。Go+ 是一门结合了 Go 的高效工程开发特性和 Python 数据科学功能的编程语言。 ... [详细]
  • 本文详细介绍了 GWT 中 PopupPanel 类的 onKeyDownPreview 方法,提供了多个代码示例及应用场景,帮助开发者更好地理解和使用该方法。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
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社区 版权所有