(快速參考)

findAllBy*

目的

使用網域類別的屬性建立查詢方法表達式,以傳回網域類別的所有相符實例的動態方法

範例

假設有這個網域類別

class Book {
    String title
    Date releaseDate
    String author
    Boolean paperback
}

以下皆為可能的方法

def results = Book.findAllByTitle("The Shining",
                  [max: 10, sort: "title", order: "desc", offset: 100])

results = Book.findAllByTitleAndAuthor("The Sum of All Fears", "Tom Clancy")

results = Book.findAllByReleaseDateBetween(firstDate, new Date())

results = Book.findAllByReleaseDateGreaterThanEquals(firstDate)

results = Book.findAllByTitleLike("%Hobbit%")

results = Book.findAllByTitleIlike("%Hobbit%") // ignore case

results = Book.findAllByTitleNotEqual("Harry Potter")

results = Book.findAllByReleaseDateIsNull()

results = Book.findAllByReleaseDateIsNotNull()

results = Book.findAllPaperbackByAuthor("Douglas Adams")

results = Book.findAllNotPaperbackByAuthor("Douglas Adams")

results = Book.findAllByAuthorInList(["Douglas Adams", "Hunter S. Thompson"])

說明

GORM 支援 動態尋找器 的概念。findAllBy* 方法會找出給定方法表達式的所有結果。

參數

  • metaParams - 包含分頁參數 maxorderoffsetsort,以及 meta 參數 readOnlytimeoutfetchSizeflushModeMap

分頁和排序參數可以用作動態方法的最後一個引數

def results = Book.findAllByTitle("The Shining",
                 [max: 10, sort: "title", order: "desc", offset: 100])

如果沒有項目符合提供的條件,則會傳回一個空清單。

可以在各自的動態方法中使用以下運算子名稱

  • LessThan

  • LessThanEquals

  • GreaterThan

  • GreaterThanEquals

  • Between

  • Like

  • Ilike(即忽略大小寫的 like)

  • IsNotNull

  • IsNull

  • Not

  • NotEqual

  • And

  • Or

  • InList

這些運算子名稱可以視為關鍵字,當查詢具有其中一個名稱作為屬性名稱的網域類別時,您會遇到問題。有關 動態尋找器 的詳細資訊,請參閱使用者指南。