【问题标题】:What is the best way to paginate results in hql在 hql 中对结果进行分页的最佳方法是什么
【发布时间】:2017-12-01 18:46:31
【问题描述】:

我想在 grails 应用程序中对结果进行分页。对于查询,我使用的是 hql。查询的工作方式如下。

List response= Order.executeQuery(selectQuery + queryWhere, [offset:command.offset, max:command.max])

但是,仅显示过滤后的值是不够的。我还必须显示查询带来了多少结果而无需过滤。此刻

def count=Order.executeQuery("select count(*)" + queryWhere)

为了提高效率,我想知道是否可以制作某种技巧并只调用一次查询?因为基本上,我两次调用相同的查询。我正在寻找最有效的方法。

【问题讨论】:

    标签: sql performance grails pagination hql


    【解决方案1】:

    我想建议使用 createCriteria 方法使用条件 API 在数据库中搜索记录。这是示例,例如:

     def orderListCriteria = Order.createCriteria()
        def list = orderListCriteria.list(max:max,offset:offset) {
                    eq("user",user)
                    eq("",)   //actual filter query
                    order(,)
                }
       def count = list.totalCount // this will give total count 
    

    【讨论】:

    • 感谢您的建议,我需要 sql 或 hql 中的解决方案。问候,
    • 如何使用 HQL 实现这一点?
    猜你喜欢
    • 2010-09-11
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    相关资源
    最近更新 更多