(快速引用)

findAll

目的

查找与指定查询匹配的所有域实例类

示例

// everything
Book.findAll()

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

// 10 books from Dan Brown staring from 5th book ordered by release date
Book.findAll("from Book as b where b.author=? order by b.releaseDate",
             ['Dan Brown'], [max: 10, offset: 5])

// examples with max/offset usage
def query = "from Book as b where b.author='Dan Brown' order by b.releaseDate"

// first 10 books
Book.findAll(query, [max: 10])

// 10 books starting from 5th
Book.findAll(query, [max: 10, offset: 5])

// examples with named parameters
Book.findAll("from Book as b where b.author=:author",
             [author: 'Dan Brown'])
Book.findAll("from Book as b where b.author=:author",
             [author: 'Dan Brown'], [max: 10, offset: 5])
Book.findAll("from Book as b where b.author in (:authors)",
             [authors: ['Dan Brown', 'Jack London']])

// examples with tables join
Book.findAll("from Book as b where not exists " +
                "(from Borrow as br where br.book = b)")

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

// Use where criteria (since Grails 2.0)
def results = Person.findAll {
     lastName == "Simpson"
}
def results = Person.findAll(sort:"firstName") {
     lastName == "Simpson"
}

描述

findAll 方法可以使用 Hibernate 的查询语言 HQL 和按示例进行查询,并返回所有匹配实例。分页可以使用 max and offset 参数进行控制

Book.findAll("from Book as b where b.author=:author",
             [author: 'Dan Brown'], [max: 10, offset: 5])

findAll 方法支持第 2 层缓存

Book.findAll("from Book as b where b.author=:author",
             [author:'Dan Brown'], [max: 10, offset: 5, cache: true])

此方法的基本语法为

Book.findAll()
Book.findAll(String query)
Book.findAll(String query, Collection positionalParams)
Book.findAll(String query, Collection positionalParams, Map queryParams)
Book.findAll(String query, Map namedParams)
Book.findAll(String query, Map namedParams, Map queryParams)
Book.findAll(Book example)
Book.findAll(Closure whereCriteria)
Book.findAll(Map queryParams, Closure whereCriteria)
如果您的 HQL 中有联接,该方法将返回一个包含域类实例的 `Array` 的 List。在这种情况下,您可能希望使用 executeQuery,以便在指定要返回的内容时有更大的灵活性。

参数

  • query - HQL 查询

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

  • namedParams - HQL 查询的命名参数的 Map

  • queryParams - 包含 'max' 和/或 'offset' 和/或 'cache' 参数的 Map

  • example - 按示例进行查询的域类的实例

  • readOnly - 如果不应该自动对返回的对象进行脏检查(类似于 read()),则为 true

  • fetchSize - 底层 JDBC 驱动每次往返获取的行数

  • flushMode - Hibernate FlushMode 覆盖,默认为 FlushMode.AUTO

  • timeout - 查询超时(以秒为单位)