Grails findAll with sort, order, max and offset?

前端 未结 4 1502
时光取名叫无心
时光取名叫无心 2021-02-19 18:25

I want to integrate sort, order, max and offset in a findAll query. The following works fine:

def books = Book.findAll(\"from Book as b where b.approved=true ord         


        
4条回答
  •  失恋的感觉
    2021-02-19 18:46

    I am assuming you are calling fetching the list of books in a controller or a service class.

    If you are calling this from a controller action, then a magic variable "params" is already available to you. For example, if you request the page as follows

    book/list?max=10&offset=2
    

    then "params" will already have those values mapped automagically.

    You can add more items to the params map as follows

    params.sort = "dateCreated"
    params.order = "desc"
    

    Once you have build your params as desired, then you can use Grails dynamic query as follows

    def books = Book.findAllByApproved(true, params)
    // use "books" variable as you wish
    

提交回复
热议问题