gpt4 book ai didi

grails - 使用迭代gorm查询聚集进行分页

转载 作者:行者123 更新时间:2023-12-02 14:17:14 25 4
gpt4 key购买 nike

我有这个controller.method渲染与配置文件关联的所有东西,并且具有gsp中分页的正确偏移量和最大值

    if (profil) {
def max = params.max ? params.max as int : 5
def offset = params.offset ? params.offset as int : 0
List things = []

things = Things.findAllByProfil(profil,[max: max, offset: offset, sort: 'dtCreated', order: 'desc'])
[profil: profil, things: things, thingsCount: things.size()]
}

但是现在我有一个特殊的“aggregating-Profile”,其中有一些关联的配置文件,我想将所有关联的配置文件的所有内容都放在一个列表中,例如:
    if (profil) {
def max = params.max ? params.max as int : 5
def offset = params.offset ? params.offset as int : 0
List things = []

if(profil.typ == 'Aggregate'){
List profiles = profil.profiles?.collect { Profil.collection.findOne(_id:it.id) }
profiles.each() { pr ->
if(pr) {
things+= Things.findAllByProfil(pr as Profil,[max: max,offset: offset, sort: 'dtCreated', order: 'desc'])
}
}
things.sort{a,b-> b.dtCreated<=>a.dtCreated}
}else{

things = Things.findAllByProfil(profil,[max: max, offset: offset, sort: 'dtCreated', order: 'desc'])
}
[profil: profil, things: things, thingsCount: things.size()]
}

但是通过这种方式,我对每个关联的配置文件多次使用了 offsetmax,因此结果列表太大。

不幸的是结果设计应该保持不变,所以 assert params.max == 5 && profil.typ == "Aggregate",第一页的结果是一个列表,其中包含所有配置文件的5个最新内容(因为我将它们全部放在列表中并按dtCreated排序),而我问题可能是:如何对聚合列表应用相同的 slice 逻辑(以及如何以高效的方式聚合事物)

解决这个问题的最佳方法是什么?

任何暗示提前致谢

最佳答案

首先,我想对rcgeorge23所说的内容进行扩展,您最好让gorm /数据库处理您的聚合。 Section 6 of the Grails documentation将带您走很长一段路。

在您的特定情况下,您可以将简单的内置比较器与您的`findAllBy'一起使用来处理聚合,这是一个具体示例:

if(profil.typ == 'Aggregate'){
List profiles = //some code to get a list of profiles
def things = Things.findAllByProfilInList(profiles, [max: max....order: 'desc'])
} else {
...
}

其次,您对分页的使用有些偏离。从技术上来讲,thingsCount应该是一个与您的条件匹配的简单“选择计数”,但是您返回的是由“max”限制的匹配条件。因此,将以下内容用于事物计数
def thingsCount = Things.countByProfilInList(profiles) //for all things with aProfile in list
or
def thingsCount = Things.countByProfil(aProfile) //for all things with aProfile

关于grails - 使用迭代gorm查询聚集进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17260580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com