(快速参考)

查找

目的

查找给定查询的第一个匹配结果,如果没有找到任何实例,则返回null

示例

// Dan brown's first book
Book.find("from Book as b where b.author='Dan Brown'")

// with a positional parameter
Book.find("from Book as b where b.author=?", ['Dan Brown'])

// with a named parameter
Book.find("from Book as b where b.author=:author", [author: 'Dan Brown'])

// use the query cache
Book.find("from Book as b where b.author='Dan Brown'", [cache: true])
Book.find("from Book as b where b.author=:author",
          [author: 'Dan Brown'],
          [cache: true])

// query by example
def example = new Book(author: "Dan Brown")
Book.find(example)

// Using where criteria (since Grails 2.0)
Person p = Person.find { firstName == "Bart" }

描述

find方法允许使用Hibernate的查询语言HQL和按示例查询查询。基本语法是

Book.find(String query)
Book.find(String query, Collection positionalParams)
Book.find(String query, Collection positionalParams, Map queryParams)
Book.find(String query, Map namedParams)
Book.find(String query, Map namedParams, Map queryParams)
Book.find(Book example)
Book.find(Closure whereCriteria)

参数

  • query - 一个HQL查询

  • positionalParams - 一个按位置参数化HQL查询的List

  • namedParams - 一个HQL查询的命名字段

  • queryParams - 一个查询参数的Map。目前,仅支持cache

  • example - 一个用于按示例查询的领域类的实例