`
brimstone
  • 浏览: 29413 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Grails 复杂查询及分页排序

阅读更多

最近在使用Idea编写Grails应用程序,Grails的脚手架确实非常方便,写个domain 增删改查一键完成,但是在项目中我们经常希望查询和list页面集成到一起而不必再弄个单独的查询页,另外查询的结果也支持分页与排序,下面是解决方案

 

控制器中的list

def list = {
    params.offset = params.offset ? params.offset : 0
    params.max = params.max ? params.max : 10
    params.sort = params.sort ? params.sort : "id"
    params.order = params.order ? params.order : "DESC"
    params.linkmanName = params.linkmanName ? params.linkmanName : ""
    params.communications = params.communications ? params.communications : ""
    params.address = params.address ? params.address : ""
    //if (params.adminName) {
    def criteria = Linkman.createCriteria()
    def results = criteria {
      and {
        like('linkmanName', '%' + params.linkmanName + '%')
        like('communications', '%' + params.communications + '%')
        like('address', '%' + params.address + '%')
      }
      maxResults(10)
      int _offset = params.offset ? Integer.parseInt(params.offset) : 0
      firstResult(_offset)
      order("${params.sort}", "${params.order}")
    }
    def _totalCount = Linkman.executeQuery("select count(*) as c from Linkman a where a.linkmanName like '%" + params.linkmanName + "%' and a.communications like '%" + params.communications + "%' and a.address like '%" + params.address + "%'")
    render(view: 'list', model: [linkmanInstanceList: results, linkmanInstanceTotal: _totalCount.getAt(0)])
  }

关于查询在我们这里有3个查询条件,通过criteria 查询结果在Grails1.1文档中就有范例,但是并不能直接得到符合查询条件的结果总数,开始我妄图使用countBy*动态查询器,诸如Linkman.countByLinkmanNameLikeAndCommunicationsLikeAndAddressLike()来做查询,但是从Hibernate输出的SQL语句发现 countBy*动态查询器最多只能支持2个条件(没找到相关说明,仅根据输出的SQL语句判断),所以只能采用别的办法,就是使用executeQuery 手工编写查询语句。

 

 

list页面

<g:form action="list" method="post">
    联系人姓名:<input type="text" name="linkmanName" value="${params.linkmanName}"/>
    通讯工具:<input type="text" name="communications" value="${params.communications}"/>
    单位或住址:<input type="text" name="address" value="${params.address}"/>
    <input class="save" type="submit" value="查询"/>
  </g:form>
</div>
<div class="content">
  <table class="data_line" cellSpacing="1" cellPadding="0" width="100%">
    <tbody>
    <tr><td align="center" bgColor="#ececec">
      <table cellSpacing="1" cellPadding="2" width="98%">
        <tbody><tr>
          <td align="right"><div align="center">
            <table height="28" cellSpacing="0" cellPadding="0" width="100%" border="0">
              <tbody><tr vAlign="middle">
                <td>&nbsp;联系人列表</td><td align="right">当前共有${linkmanInstanceTotal}条记录</td>
              </tr></tbody></table></div></td></tr></tbody></table>
      <table class="data_tab" cellSpacing="1" cellPadding="2" width="98%">
        <thead class="data_th">
        <tr align="center">
          <td width="40">序号</td>
          <g:sortableColumn property="linkmanName" title="联系人姓名" params="${params}"/>
          <g:sortableColumn property="linkmanSex" title="性别" params="${params}"/>
          <g:sortableColumn property="address" title="单位或住址" params="${params}"/>
          <td>关系人</td>
          <td width="100">操作</td>
        </tr>
        </thead>
        <tbody class="data_tb">
        <g:if test="${flash.message}">
          <div class="message">${flash.message}</div>
        </g:if>

        <g:each in="${linkmanInstanceList}" status="i" var="linkmanInstance">
          <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">
            <td align="center">${++i + (params.offset ? Integer.parseInt(params.offset) : 0)}</td>
            <td>&nbsp;<g:link action="show" id="${linkmanInstance.id}">${fieldValue(bean: linkmanInstance, field: 'linkmanName')}</g:link></td>
            <td>&nbsp;${fieldValue(bean: linkmanInstance, field: 'linkmanSex')}</td>
            <td>&nbsp;${fieldValue(bean: linkmanInstance, field: 'address')}</td>
            <td>&nbsp;<g:link action="show" controller="suspect" id="${linkmanInstance.suspectId}">${fieldValue(bean: linkmanInstance, field: 'suspect')}</g:link></td>
            <td>&nbsp;
            <g:if test="${session.loginUser.adminLimits.indexOf('canEdit')>=0}">
              <g:link action="edit" id="${linkmanInstance.id}">修改</g:link>
            </g:if>
            <g:if test="${session.loginUser.adminLimits.indexOf('canDelete')>=0}">
              <g:link action="delete" id="${linkmanInstance.id}" onclick="return confirm('确定删除?');">删除</g:link>
            </g:if>
            </td>
          </tr>
        </g:each>
        </tbody></table>
      <table cellSpacing="1" cellPadding="2" width="98%">
        <tbody>
        <tr><td align="right">
          <div align="right">
            <table height="28" cellSpacing="0" cellPadding="0"
                    width="100%" border="0">
              <tbody>
              <tr vAlign="middle">
                <div class="paginateButtons">
                  <g:paginate total="${linkmanInstanceTotal}" params="${params}"/>
                </div>
              </tr></tbody></table></div></td></tr></tbody></table></td></tr></tbody>
  </table>

 

页面上要注意的就是 使用 params="${params}" 传递查询、分页、排序参数。

 

分享到:
评论
3 楼 rqfreedom 2012-02-11  
1.<g:form action="list" method="post">  
2.    联系人姓名:<input type="text" name="linkmanName" value="${params.linkmanName}"/>  
3.    通讯工具:<input type="text" name="communications" value="${params.communications}"/>  
4.    单位或住址:<input type="text" name="address" value="${params.address}"/>  
5.    <input class="save" type="submit" value="查询"/>  
6.  </g:form>  
7.</div>  
8.<div class="content">  
9.  <table class="data_line" cellSpacing="1" cellPadding="0" width="100%">  
10.    <tbody>  
11.    <tr><td align="center" bgColor="#ececec">  
12.      <table cellSpacing="1" cellPadding="2" width="98%">  
13.        <tbody><tr>  
14.          <td align="right"><div align="center">  
15.            <table height="28" cellSpacing="0" cellPadding="0" width="100%" border="0">  
16.              <tbody><tr vAlign="middle">  
17.                <td>&nbsp;联系人列表</td><td align="right">当前共有${linkmanInstanceTotal}条记录</td>  
18.              </tr></tbody></table></div></td></tr></tbody></table>  
19.      <table class="data_tab" cellSpacing="1" cellPadding="2" width="98%">  
20.        <thead class="data_th">  
21.        <tr align="center">  
22.          <td width="40">序号</td>  
23.          <g:sortableColumn property="linkmanName" title="联系人姓名" params="${params}"/>  
24.          <g:sortableColumn property="linkmanSex" title="性别" params="${params}"/>  
25.          <g:sortableColumn property="address" title="单位或住址" params="${params}"/>  
26.          <td>关系人</td>  
27.          <td width="100">操作</td>  
28.        </tr>  
29.        </thead>  
30.        <tbody class="data_tb">  
31.        <g:if test="${flash.message}">  
32.          <div class="message">${flash.message}</div>  
33.        </g:if>  
34.  
35.        <g:each in="${linkmanInstanceList}" status="i" var="linkmanInstance">  
36.          <tr class="${(i % 2) == 0 ? 'odd' : 'even'}">  
37.            <td align="center">${++i + (params.offset ? Integer.parseInt(params.offset) : 0)}</td>  
38.            <td>&nbsp;<g:link action="show" id="${linkmanInstance.id}">${fieldValue(bean: linkmanInstance, field: 'linkmanName')}</g:link></td>  
39.            <td>&nbsp;${fieldValue(bean: linkmanInstance, field: 'linkmanSex')}</td>  
40.            <td>&nbsp;${fieldValue(bean: linkmanInstance, field: 'address')}</td>  
41.            <td>&nbsp;<g:link action="show" controller="suspect" id="${linkmanInstance.suspectId}">${fieldValue(bean: linkmanInstance, field: 'suspect')}</g:link></td>  
42.            <td>&nbsp;   
43.            <g:if test="${session.loginUser.adminLimits.indexOf('canEdit')>=0}">  
44.              <g:link action="edit" id="${linkmanInstance.id}">修改</g:link>  
45.            </g:if>  
46.            <g:if test="${session.loginUser.adminLimits.indexOf('canDelete')>=0}">  
47.              <g:link action="delete" id="${linkmanInstance.id}" onclick="return confirm('确定删除?');">删除</g:link>  
48.            </g:if>  
49.            </td>  
50.          </tr>  
51.        </g:each>  
52.        </tbody></table>  
53.      <table cellSpacing="1" cellPadding="2" width="98%">  
54.        <tbody>  
55.        <tr><td align="right">  
56.          <div align="right">  
57.            <table height="28" cellSpacing="0" cellPadding="0"  
58.                    width="100%" border="0">  
59.              <tbody>  
60.              <tr vAlign="middle">  
61.                <div class="paginateButtons">  
62.                  <g:paginate total="${linkmanInstanceTotal}" params="${params}"/>  
63.                </div>  
64.              </tr></tbody></table></div></td></tr></tbody></table></td></tr></tbody>  
65.  </table>
2 楼 rqfreedom 2012-02-11  
1.<g:form action="list" method="post"> 
2.    联系人姓名:<input type="text" name="linkmanName" value="${params.linkmanName}"/> 
3.    通讯工具:<input type="text" name="communications" value="${params.communications}"/> 
4.    单位或住址:<input type="text" name="address" value="${params.address}"/> 
5.    <input class="save" type="submit" value="查询"/> 
6.  </g:form> 
7.</div> 
8.<div class="content"> 
9.  <table class="data_line" cellSpacing="1" cellPadding="0" width="100%"> 
10.    <tbody> 
11.    <tr><td align="center" bgColor="#ececec"> 
12.      <table cellSpacing="1" cellPadding="2" width="98%"> 
13.        <tbody><tr> 
14.          <td align="right"><div align="center"> 
15.            <table height="28" cellSpacing="0" cellPadding="0" width="100%" border="0"> 
16.              <tbody><tr vAlign="middle"> 
17.                <td>&nbsp;联系人列表</td><td align="right">当前共有${linkmanInstanceTotal}条记录</td> 
18.              </tr></tbody></table></div></td></tr></tbody></table> 
19.      <table class="data_tab" cellSpacing="1" cellPadding="2" width="98%"> 
20.        <thead class="data_th"> 
21.        <tr align="center"> 
22.          <td width="40">序号</td> 
23.          <g:sortableColumn property="linkmanName" title="联系人姓名" params="${params}"/> 
24.          <g:sortableColumn property="linkmanSex" title="性别" params="${params}"/> 
25.          <g:sortableColumn property="address" title="单位或住址" params="${params}"/> 
26.          <td>关系人</td> 
27.          <td width="100">操作</td> 
28.        </tr> 
29.        </thead> 
30.        <tbody class="data_tb"> 
31.        <g:if test="${flash.message}"> 
32.          <div class="message">${flash.message}</div> 
33.        </g:if> 
34. 
35.        <g:each in="${linkmanInstanceList}" status="i" var="linkmanInstance"> 
36.          <tr class="${(i % 2) == 0 ? 'odd' : 'even'}"> 
37.            <td align="center">${++i + (params.offset ? Integer.parseInt(params.offset) : 0)}</td> 
38.            <td>&nbsp;<g:link action="show" id="${linkmanInstance.id}">${fieldValue(bean: linkmanInstance, field: 'linkmanName')}</g:link></td> 
39.            <td>&nbsp;${fieldValue(bean: linkmanInstance, field: 'linkmanSex')}</td> 
40.            <td>&nbsp;${fieldValue(bean: linkmanInstance, field: 'address')}</td> 
41.            <td>&nbsp;<g:link action="show" controller="suspect" id="${linkmanInstance.suspectId}">${fieldValue(bean: linkmanInstance, field: 'suspect')}</g:link></td> 
42.            <td>&nbsp;  
43.            <g:if test="${session.loginUser.adminLimits.indexOf('canEdit')>=0}"> 
44.              <g:link action="edit" id="${linkmanInstance.id}">修改</g:link> 
45.            </g:if> 
46.            <g:if test="${session.loginUser.adminLimits.indexOf('canDelete')>=0}"> 
47.              <g:link action="delete" id="${linkmanInstance.id}" onclick="return confirm('确定删除?');">删除</g:link> 
48.            </g:if> 
49.            </td> 
50.          </tr> 
51.        </g:each> 
52.        </tbody></table> 
53.      <table cellSpacing="1" cellPadding="2" width="98%"> 
54.        <tbody> 
55.        <tr><td align="right"> 
56.          <div align="right"> 
57.            <table height="28" cellSpacing="0" cellPadding="0" 
58.                    width="100%" border="0"> 
59.              <tbody> 
60.              <tr vAlign="middle"> 
61.                <div class="paginateButtons"> 
62.                  <g:paginate total="${linkmanInstanceTotal}" params="${params}"/> 
63.                </div> 
64.              </tr></tbody></table></div></td></tr></tbody></table></td></tr></tbody> 
65.  </table>
1 楼 rqfreedom 2012-02-11  
system.out.println(123)

相关推荐

Global site tag (gtag.js) - Google Analytics