gpt4 book ai didi

grails - Grails如何根据变化的搜索参数动态搜索

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

假设我们必须对一堆数据库记录执行搜索,并且搜索条件由几个复选框和一个文本字段定义。因此,如果选中了“美国公民”复选框,并且文本字段“名称”中填充了“John Doe”。然后,我在Grails应用程序中的搜索将类似于:

def results = IndividualRecord.findAllWhere(citizenship: 'US', name: 'John Doe')

现在,如果有人还选中了“voter”框,那么我将搜索范围更改为:
def results = IndividualRecord.findAllWhere(citizenship: US, name: 'John Doe', voter: true)

实际上,我的应用程序有几个文本字段和6个复选框。显然,我无法根据条件的每种不同组合编写自定义搜索功能,因为组合太多。解决这个问题的有效方法是什么?因此,我想理想情况下应该有一个搜索功能可以接受自定义的查询参数并使用这些参数进行搜索。我有点迷茫和困惑。任何帮助表示赞赏。

最佳答案

In reality my application has a couple of text fields and 6 checkboxes. Obviously I cannot write a custom search function based on each different combination of criteria there's just too many combinations



您实际上并不需要担心处理组合。如果您可以通过 map 接收字段名称和值,则只需将 map 传递给 findAllWhere,例如
def search(Map predicates) {
IndividualRecord.findAllWhere(predicates)
}

如果由于某种原因您不喜欢这种方法,则可以改用条件查询,例如
def search(Map predicates) {

IndividualRecord.withCriteria {

if (predicates.voter != null) {
eq 'voter', predicates.voter
}

if (predicates.citizenship) {
eq 'citizenship', predicates.citizenship
}

if (predicates.name) {
eq 'name', predicates.name
}
}
}

关于grails - Grails如何根据变化的搜索参数动态搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25099536/

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