grails中的动态方法查询提供分页设置

1. findAll
通过写HQL语句查询符合条件的记录如:
Book.findAll("fromBook as b where b.author=:author",[author:'Dan Brown'], [max:10, offset:5])
使用了参数作为查询条件,同时提供了分页的参数map:[max:10, offset:5]
2.findAllBy
比较常用的动态查询方法之一,可实现分页查询如:
Book.findAllByTitle("TheShining",[max:10, sort:"title",order:"desc",offset:100] )
指定查询条件,按Title=TheShining查询,同时指定分页参数map:[max:10,sort:"title",order:"desc",offset:100]
3.list
list也是比较常用的动态查询方法之一,列出所有实例如:
def results = Book.list(max:10, offset:100,sort:"title",order:"desc")
可以指定分页参数
4.criteria.list
比较常用的查询方法之一,可以在list中指定查询闭包,和分页参数map如:
def c = Goods.createCriteria()
       params.sort= "price"
        params.order= "asc"
        defgoodList = c.list (params, searchClosure)
其中,params为分页参数,searchClosure为查询条件闭包

def search = {
 
       if(!params.max)params.max = 10
        if(!params.offset)params.offset = 0
        defsearchClosure =  {
            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}")
            }
        }
        defc = Goods.createCriteria();
        params.sort= "price"
        params.order= "asc"
        defgoodList = c.list (params, searchClosure)
//        defgoodsCount = c.count (searchClosure)
        defgoodsCount = goodList.totalCount
        printlngoodsCount
        render(view: 'list', model: [goodsInstanceList: goodList,goodsInstanceTotal: goodsCount])
    }

使用闭包封装查询条件如上面的searchClosure闭包,这样就可以重复使用查询条件了,
在list的时候会返回一个PagedResultList类型,里面就有totalCount属性,表示符合查询条件的记录数,这样就不用count了。在list的时候可以传入分页参数组成的map如上面的params参数,这样就不用在查询闭包里设置分页参数了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值