gpt4 book ai didi

Grails Plugin Searchable - 默认通配符搜索

转载 作者:行者123 更新时间:2023-12-02 07:03:21 27 4
gpt4 key购买 nike

有没有办法自动用通配符包装所有搜索?

例如:

 Book.search("*${params.q}*", params)

最佳答案

我不熟悉.search(您使用插件吗?)。但是,对于模型中的通配符搜索,我通常在域模型类中创建一个方法。在您的示例中,

在 Book 模型类中:

class Book {
String title
String author
int year

static List wildSearch(par, val) {
def foundList = this.executeQuery("select b FROM Book b WHERE ${par} like \'%${val}%\'")
return foundList
}
}

在你的 Controller 中:

def searchBook = {
def b1 = new Book(title: "Farewell To Arms", author: "Ernest Hemingway").save()
def b2 = new Book(title: "The Brother's Karamazov", author: "Anton Chekov").save()
def b3 = new Book(title: "Brothers in Arms", author: "Cherry Dalton").save()

// If you search for "Arms", This returns b1 and b3
def found = Book.wildSearch("title", params.title)
}

示例网址:

http://localhost:8080/mytest/mycontroller/searchBooks?title=Arms    

关于Grails Plugin Searchable - 默认通配符搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15389971/

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