Grails In Action-05.Retrieving the data you need

第五章的内容是在Grails中查询功能的讲解。代码中讲解了模糊查询和标准查询两个例子,知识点如下:

  • where方法,这个方法是grails2.0版本后才新加进来的,作用是比较表达中的内容,并返回结果为真的对象
  • loginId =~ “%${query}%“,=~的意思是创建一个普通的Java Matcher实例,对左右的内容进行比较、赋值
  • Profile.metaClass.properties*.name,查找元类型的属性名称
  • Profile.withCriteria,grails的标准查询
  • profileProps.grep(field) && value,元类型名称和参数名对比
  • ilike(field, value),hibernate hql的内容

先看看模糊查询。

模糊查询

新建一个查询页面,根据用户名查询用户信息。views/user/search.gsp,如果要正常访问到这个页面需要在UserController中增加一个访问闭包。

search页面


<html>
    <head>
        <title>Search Hubbub</title>
        <meta name="layout" content="main"/>
    </head>

    <body>
        <formset>
            <legend>Search for Friends</legend>
            <g:form action="results">
                <label for="query">User Id</label>
                <g:textField name="query" />
                <g:submitButton name="search" value="Search"/>
            </g:form>
        </formset>
    </body>
</html>

search闭包


......
def search = {}
......

form中action=“results”,在controller中新建一个results闭包,处理提交信息并返回给results.gsp页面。

UserController


def results(String query) {
    def users = User.where { 
        loginId =~ "%${query}%" 
    }.list()

    return [ users: users,term: params.loginId,totalUsers: User.count() ]
}

这个闭包接收query参数,返回查询到的user对象列表,参数名和统计查询到的user总数,results.gsp负责将返回的信息显示到页面

results


<html>
    <head>
        <title>Search Results</title>
        <meta name="layout" content="main"/>
    </head>

    <body>
        <h1>Results</h1>
        <p>
            Searched ${totalUsers} records
            for items matching <em>${term}</em>.
            Found <strong>${users.size()}</strong> hits.
        </p>
        <ul>
            <g:each var="user" in="${users}">
                <li><g:link controller="user" action="show" id="${user.id}">${user.loginId}</g:link></li>
            </g:each>
        </ul>
        <g:link action='search'>Search Again</g:link>
    </body>
</html>

标准查询

advSearch.gsp,查询页面


<html>
    <head>
        <title>Advanced Search</title>
        <meta name="layout" content="main"/>
    </head>

    <body>
        <formset>
            <legend>Advanced Search for Friends</legend>

            <table>
                <g:form action="advResults">
                    <tr>
                        <td>Name</td>
                        <td><g:textField name="fullName" /></td>
                    </tr>
                    <tr>
                        <td>Email</td>
                        <td><g:textField name="email" /></td>
                    </tr>
                    <tr>
                        <td>Homepage</td>
                        <td><g:textField name="homepage" /></td>
                    </tr>
                    <tr>
                        <td>Query Type:</td>
                        <td>
                            <g:radioGroup name="queryType" labels="['And','Or','Not']" values="['and','or','not']" value="and" >
                                ${it.radio} ${it.label}
                            </g:radioGroup>
                        </td>
                    </tr>

                    <tr>
                        <td/>
                        <td><g:submitButton name="search" value="Search"/></td>
                    </tr>
                </g:form>
            </table>
        </formset>
    </body>
</html>

UserController.groovy,控制器


def advSearch() {
}

def advResults() {
    def profileProps = Profile.metaClass.properties*.name
    def profiles = Profile.withCriteria {
        "${params.queryType}" {
            params.each { field, value ->
                if (profileProps.grep(field) && value) {
                    ilike(field, value)
                }
            }
        }
    }
    [ profiles : profiles ]
}

advResults.gsp,查询数据结果显示页面


<html>
    <head>
        <title>Advanced Search Results</title>
        <meta name="layout" content="main"/>
    </head>

    <body>
        <h1>Advanced Results</h1>
        <p>Searched
            for items matching <em>${term}</em>.
            Found <strong>${profiles.size()}</strong> hits.
        </p>
        <ul>
            <g:each var="profile" in="${profiles}">
                <li><g:link controller="profile" action="show" id="${profile.id}">${profile.fullName}</g:link></li>
            </g:each>
        </ul>
        <g:link action='advSearch'>Search Again</g:link>
    </body>
</html>

你可能感兴趣的:(Grails In Action-05.Retrieving the data you need)