【问题标题】:Grails Plugin Searchable - default wildcard searchGrails Plugin Searchable - 默认通配符搜索
【发布时间】:2013-03-01 15:54:20
【问题描述】:

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

例如:

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

【问题讨论】:

标签: grails searchable grails-searchable


【解决方案1】:

我不熟悉 .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    

【讨论】:

  • Searchable 是一个 grails 插件,它使搜索更容易一些。感谢您的建议,但我想知道是否有办法默认插件。
  • 应该更仔细地阅读您的问题标题。对此感到抱歉。
猜你喜欢
  • 2012-11-23
  • 1970-01-01
  • 2013-02-22
  • 2014-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多