Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to realize compound query and result pagination in criteria of grails

Shulou Source: shulou.com Published: 2022-05-31 23:30:23 09月22日 Update

This article is to share with you about grails criteria how to achieve compound query and result paging, the editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Def search = {

If (! params.max) params.max = 10

If (! params.offset) params.offset = 0

Def searchClosure = {

If (params.categoryName) {

Category {

Eq ('categoryName', params.categoryName)

}

}

If (params.title) {

Like ('title', "${params.title}")

}

If (params.priceLow) {

Ge ('price', new BigDecimal (params.priceLow))

}

If (params.priceHigh) {

Le ('price', new BigDecimal (params.priceHigh))

}

If (params.description) {

Like ('description', "${params.description}")

}

}

Def c = Goods.createCriteria ()

Params.sort = "price"

Params.order = "asc"

Def goodList = c.list (params, searchClosure)

/ / def goodsCount = c.count (searchClosure)

Def goodsCount = goodList.totalCount

Println goodsCount

Render (view: 'list', model: [goodsInstanceList: goodList, goodsInstanceTotal: goodsCount])

}

Use closures to encapsulate query conditions such as the searchClosure closure above, so that query conditions can be reused

In list, a PagedResultList type is returned with a totalCount attribute indicating the number of records that meet the query criteria, so that count is not used. When you list, you can pass in the map composed of paging parameters, such as the params parameter above, so that you do not have to set paging parameters in the query closure.

This is how grails's criteria implements compound queries and paging results. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Queries parameters conditions closures results no as above time more knowledge articles facets practical that is attributes work meetings articles look knowledge points types Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn Shulou Technology MariaDB macOS