grails 框架怎么按条件查询数据

我们需要准备三个东西:页面,控制器,实体

首先我们需要在view创建一个页面:名为index.gsp

<%@ page import="com.zfq.Book" %>
<!DOCTYPE html>
<html>
<head>
    <meta name="layout" content="main">
    <g:set var="entityName" value="${message(code: 'book.label', default: 'Book')}"/>
    <title><g:message code="default.list.label" args="[entityName]"/></title>
</head>

<body>
<a href="#list-book" class="skip" tabindex="-1"><g:message code="default.link.skip.label"
                                                           default="Skip to content&hellip;"/></a>

<div class="nav" role="navigation">
    <ul>
        <li><a class="home" href="${createLink(uri: '/')}"><g:message code="default.home.label"/></a></li>
        <li><g:link class="create" action="create"><g:message code="default.new.label"
                                                              args="[entityName]"/></g:link></li>
    </ul>
</div>

<div id="list-book" class="content scaffold-list" role="main">
%{--			<h1><g:message code="default.list.label" args="[entityName]" /></h1>--}%
    <g:if test="${flash.message}">
        <div class="message" role="status">${flash.message}</div>
    </g:if>
    <g:form url="[resource: bookInstance, action: 'queryInfoByCondition']" method="GET">

            <label for="title">
                <g:message code="book.title.label" default="Title"/>
                <span>:</span>
            </label>
            <g:textField name="title"/>
            <label for="author">
                <g:message code="book.author.label" default="Author"/>
                <span>:</span>
            </label>
            <g:textField name="author"/>

            <g:actionSubmit class="save" action="queryInfoByCondition"
                            value="Search"/>
    </g:form>

    <table>
        <thead>
        <tr>

            <g:sortableColumn property="id" title="${message(code: 'book.title.label', default: 'id')}"/>
            <g:sortableColumn property="title" title="${message(code: 'book.title.label', default: 'Title')}"/>
            <g:sortableColumn property="author" title="${message(code: 'book.author.label', default: 'Author')}"/>

        </tr>
        </thead>
        <tbody>
        <g:each in="${bookInstanceList}" status="i" var="bookInstance">
            <tr class="${(i % 2) == 0 ? 'even' : 'odd'}">

                <td><g:link action="show"
                            id="${bookInstance.id}">${fieldValue(bean: bookInstance, field: "id")}</g:link></td>
                <td><g:link action="show"
                            id="${bookInstance.id}">${fieldValue(bean: bookInstance, field: "title")}</g:link></td>

                <td>${fieldValue(bean: bookInstance, field: "author")}</td>

            </tr>
        </g:each>
        </tbody>
    </table>

    <div class="pagination">
        <g:paginate total="${bookInstanceCount ?: 0}"/>
    </div>
</div>
</body>
</html>

实体

package com.zfq

class Book {
    String id;
    String title
    String author


    static constraints = {
        title(blank: false)
        author(blank: false)
    }
    static mapping={
        id generator: "uuid.hex", type:"string"
    }
}

控制器

class BookController {
// 显示信息根据查询条件
    def queryInfoByCondition={
        if (!params.max) params.max = 10
        if (!params.offset) params.offset = 0
        if (!params.sort) params.sort = "id"
        if (!params.order) params.order = "asc"
        def cel = {
            if(params.title){
                like("title", "%${params.title}%")
            }
            if(params.author){
                like("author", "%${params.author}%")
            }
        }
        def results = Book.createCriteria().list(params,cel)
//      def cut = Book.createCriteria().count(cel)
        render(view: "index",model: [bookInstanceList: results, bookInstanceCount: results.totalCount])

    }
}

cel是数据库的查询条件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值