簡體   English   中英

Grails Plugin Searchable - 默認通配符搜索

[英]Grails Plugin Searchable - default wildcard search

有沒有辦法用通配符自動包裝所有搜索?

例如:

 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
   }
}

在你的控制器中:

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    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM