Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report