`
seanwon
  • 浏览: 63707 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

GRAILS 分页 查询

阅读更多
在做grails的查询分页的时候,发现一个问题,那就是输入的查询参数不能作为条件在分页之间传递,而且如果做分页查询的总数也不得而知,针对多条件criteria查询(也适用于HQL查询)做了如下实现,代码如下:
最主要的是蓝色标注的部分,创建新的criteria,用projection的rowCount()方法,统计总条数,查询条件要保持一致,然后制定参数传递给view。
def bookingInstance = new Booking(params)
                def criteria = Booking.createCriteria()
               
                def max = 5
                def offset = 0
                if(params.max) max = params.max.toInteger()
                if(params.offset) offset = params.offset.toInteger()
                def results = criteria {
                    and {
                        if(bookingInstance.employee && session.employee.role=='Admin'){
                            eq('employee',bookingInstance.employee)
                        }
                        if(bookingInstance.meetingRoom){
                            eq('meetingRoom',bookingInstance.meetingRoom)
                        }
                        if(bookingInstance.startDateTime){
                            println bookingInstance.startDateTime
                            ge('endDateTime',bookingInstance.startDateTime)
                        }
                        if(bookingInstance.endDateTime){
                            le('startDateTime',bookingInstance.endDateTime+1)
                        }
                        maxResults(max)
                        firstResult(offset)
                        order('startDateTime', 'asc')
                    }
                }
                def criteria1 = Booking.createCriteria()
                def branchCount = criteria1.get {

                    and {
                        if(bookingInstance.employee && session.employee.role=='Admin'){
                            eq('employee',bookingInstance.employee)
                        }
                        if(bookingInstance.meetingRoom){
                            eq('meetingRoom',bookingInstance.meetingRoom)
                        }
                        if(bookingInstance.startDateTime){
                            println bookingInstance.startDateTime
                            ge('endDateTime',bookingInstance.startDateTime)
                        }
                        if(bookingInstance.endDateTime){
                            le('startDateTime',bookingInstance.endDateTime+1)
                        }
                    }
                    projections {
                        rowCount()
                    }
                }
                params.branchCount = branchCount
                 params.page = 1
                println "${branchCount}nnnn"    
                render(view:'searchresults',model:[ bookingInstanceList: results,params:params ])


view的代码如下:
<g:paginate controller="booking"
                        action="search" total="${params.branchCount}"
                        max="5" maxsteps="4" params="${params}" />
主要是指定max,params,total参数,这样就可以在分页查询中传递参数了。
分享到:
评论
7 楼 take 2012-03-08  
理论上是可以返回个PagedResultList类型对象,里面可以包含List和count.
但我现在只要返回PagedResultList, 程序就会报错.!
6 楼 seanwon 2010-08-04  
eye4sany 写道
不觉得有一大段的代码写了两遍吗

上面一段是演示不分页的,下面一段是演示分页的。。

不过这个方案不是最好的了,看一下SSailYang的回复:

SSailYang 写道
不用这么麻烦。如果你向 list 方法中传递分页信息
list(max:x, offset:x) {
  // query criteria
}

list 方法将返回 PagedResultList 类型的对象。你可以通过 PagedResultList 中的 totalCount 属性得到记录的总数。

5 楼 eye4sany 2010-08-04  
不觉得有一大段的代码写了两遍吗
4 楼 java-007 2009-11-05  
thanks!
3 楼 seanwon 2009-01-28  
我搞明白了,SSailYang您太有才了,这个我找了很多api和参考,竟然没有找到。

如果有相应的参考文档,还请共享。

criteria的list方法,如果指定max:x,offset:y参数,返回的是PageResultList类型,如果不指定,是返回的ArrayList类型。

这太有才了。
2 楼 seanwon 2009-01-28  
SSailYang 写道

不用这么麻烦。如果你向 list 方法中传递分页信息


Java代码

list(max:x,&nbsp;offset:x)&nbsp;{ &nbsp;&nbsp;
&nbsp;&nbsp;//&nbsp;query&nbsp;criteria &nbsp;&nbsp;
}&nbsp;&nbsp;list(max:x, offset:x) {
  // query criteria
}
list 方法将返回 PagedResultList 类型的对象。你可以通过 PagedResultList 中的 totalCount 属性得到记录的总数。


我可能方法不对,但是按照你提到的做了,好像没有totalCount这么一个属性。
1 楼 SSailYang 2009-01-28  
不用这么麻烦。如果你向 list 方法中传递分页信息
list(max:x, offset:x) {
  // query criteria
}

list 方法将返回 PagedResultList 类型的对象。你可以通过 PagedResultList 中的 totalCount 属性得到记录的总数。

相关推荐

    grails实现分页技术

    供初学者使用,在grails中只有list(自动生成)中有分页实现,而自己新增的页面中需自己实现分页,该文档中描述了如何实现分页以及注意点

    Grails GORM查询.pdf

    简单介绍grails的查询操作,已经类似与hibnatate的查询方式

    Grails Grails Grails

    Grails Grails Grails Grails Grails

    Grails权威指南 Grails权威指南

    Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南Grails权威指南

    grails框架数据查询

    NULL 博文链接:https://niuniufei.iteye.com/blog/2292361

    grails快捷键

    常用的grails的开发的快捷键基本都在里面的 本来应该是免费使用分享,希望得点积分学更多的东西来分享欢迎下载

    Eclipse下搭建Grails项目

    Grails项目的应用越来越多,而对于初学者来说,在Eclipse下搭建Grails项目是一个难题,这个文档将教会你如何搭建Grails项目,希望对你有所帮助。

    Grails权威指南

     8.6 对数据进行分页  8.7 创建自定义标签  8.7.1 创建标签库  8.7.2 自定义标签基础内容  8.7.3 演示自定义标签  8.7.4 测试自定义标签  8.8 本章小结 第9章 ajax  9.1 ajax基础...

    Grails入门指南 -- 针对grails1.0.4更新

    Grails入门指南中文pdf -- 针对grails1.0.4更新,附加idea8 开发grails的流程

    Grails1.1中文文档

    Grails1.1中文文档

    grails+Xfire webservice

    grails+Xfire webservice

    Groovy轻松入门——Grails实战基础篇

    在学习任何东西之前,最重要的是培养兴趣,Groovy世界最耀眼的技术之一--Grails相信大家早已耳闻,我将通过Grails实战系列文章 向您展现Grails的迷人风采,使您感受到Grails的魅力,以至疯狂地爱上Grails,并坠入...

    grails

    grails-2.1.zip.001

    grails开发环境配置及应用开发

    详细讲解grails开发环境配置。 详细讲解grails连接mysql数据库,crud开发

    grails 1.0.4

    Grails专为下一代JavaWeb应用程序而设计的框架,其借助于Groovy动态语言,使Web开发变得简单而方便。Grails尽量为更多现有的Java项目创建一个全面的框架(不仅局限于视图处理),这和当前一些Java框架提供给用户的一...

    Grails中文参考手册

    Grails 中文 参考手册

    grails入门经典

    grails grails入门经典 grails入门 grails例子 grails资料 通过自学一点点积累起来的,相信对你有帮助的。

    Grails1.3.7参考手册

    Grails 1.3.7英文版官方参考手册,学习Grails的权威指南

    grails 中文文档+grails-fckeditor-0.9.5.zip插件

    grails 中文文档+grails-fckeditor-0.9.5.zip插件

Global site tag (gtag.js) - Google Analytics