上一节介绍了一个基本的动态查询器是怎么工作的,grails还有很多的动态查询器,我们来建一个测试来测试一下其他查询器的工作状况
1、创建一个集成测试类
grails create-integration-test com.grailsinaction.QueryIntegration
创建一个测试方法testBasicDynamicFinders
1 package com.grailsinaction
2
3 import static org.junit.Assert.*
4 import org.junit.*
5
6 class QueryIntegrationTests {
7
8 @Before
9 void setUp() {
10 // Setup logic here
11 }
12
13 @After
14 void tearDown() {
15 // Tear down logic here
16 }
17
18 @Test
19 void testBasicDynamicFinders() {
20 new User(userId: 'glen', password: 'secret', profile: new Profile(email: 'glen@glensmith.com')).save()
21 new User(userId: 'peter', password: 'sesame', profile: new Profile(homepage: 'http://www.peter.com/')).save()
22 def user = User.findByPassword('sesame')
23 assertEquals 'peter', user.userId
24
25 user = User.findByUserIdAndPassword('glen', 'secret')
26 assertEquals 'glen', user.userId
27
28 def now = new Date()
29 def users = User.findAllByDateCreatedBetween(now-1, now)
30 assertEquals 2, users.size()
31
32 def profiles = Profile.findAllByEmailIsNotNull()
33 assertEquals 1, profiles.size()
34 }
35 }
2、一些常用的查询器
操作名 |
示例 |
注释 |
LessThan |
User.findAllByDateCreatedLessThan(lastWeek) |
小于给定值 |
LessThanEquals |
User.findAllByDateCreatedLessThanEquals(lastWeek) |
小于或等于给定值 |
GreaterThan |
User.findAllByDateCreatedGreaterThan(lastWeek) |
大于给定值 |
GreaterThanEquals |
User.findAllByDateCreatedGreaterThanEquals(lastWeek) |
大于或等于给定值 |
Like |
User.findAllByPasswordLike("secret") |
等价于 SQL like 表达式 |
Ilike |
User.findAllByPasswordIlike("Secret") |
类似于Like ,但不是大小写敏感 |
NotEqual |
User.findAllByPasswordNotEqual("password") |
不等于 |
Between |
User.findAllByDateCreatedBetween(lastWeek, now) |
等于两个值之间 (需要两个参数) |
IsNotNull |
User.findAllByPasswordIsNotNull() |
不为null的值 (不需要参数) |
IsNull |
User.findAllByPasswordIsNull() |
为null的值 (不需要参数) |
And |
User.findAllByDateCreatedGreaterThanAndUserIdLike(lastWeek, 'glen') |
|
Or |
User.findAllByPasswordIsNullOrPasswordLike("secret") |
|
3、动态查询器很强大,但是也有局限性。最明显的是只能查询两个字段,如果要用更多的查询条件,则需要使用Hibernate的HQL语法查询。关于HQL后面的章节再专门了解。
先了解QBE、实例查询,这里建立一个集成测试方法来了解QBE
在com.grailsinaction.QueryIntegration中增加一个testQueryByExample测试方法
1 @Test
2 void testQueryByExample() {
3 new User(userId: 'glen', password: 'password').save()
4 new User(userId: 'peter', password: 'password').save()
5 new User(userId: 'cynthia', password: 'sesame').save()
6
7 def userToFind = new User(userId: 'glen')
8 def u1 = User.find(userToFind)
9 assertEquals('password', u1.password)
10
11 userToFind = new User(userId: 'cynthia')
12 def u2 = User.find(userToFind)
13 assertEquals('cynthia', u2.userId)
14
15 userToFind = new User(password: 'password')
16 def u3 = User.findAll(userToFind)
17 assertEquals(['glen', 'peter'], u3*.userId)
18 }
在这个测试方法里我们可以看到,可以将一个对象作为参数,进行数据查询
4、我们已经有一个非常好用的find方法,但是有时候需要得到list、count呢,如:我们想得到前5个用户的列表信息,可以这样写
def users = User.list([sort: 'userId', order: 'asc',max: 5, fetch: [posts: 'eager']])
list()方法可带的选项参数如下:
选项 |
简介 |
max |
返回指定的最大行数 |
sort |
按指定的字段进行排序 |
order |
按指定的顺序排序,desc:降序,asc:升序 |
ignoreCase |
忽略大小写过滤 |
fetch |
懒加载策略 |
offset |
返回值指定元素从哪里开始,主要用于分页 |
分享到:
相关推荐
groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part1 共两个压缩包,解压后将扩展名.zip.bak改为.zip再次解压。
elasticsearch-grails-plugin, 恢复的ElasticSearch grails插件 Elasticsearch插件插件这个项目是一个基于Elasticsearch的插件,这个项目是基于的人完成的伟大工作的。你为什么想要为原来的Elasticsearch插件提供...
groovy-grails-tool-suite-3.6.4.RELEASE-e4.4.2-win32-x86_64.part2 共两个包,解压后需要将扩展名.zip.bak改名为.zip重新解压。 http://dist.springsource.com/release/STS/3.8.1.RELEASE/dist/ e4.6/spring-tool-...
Grails In Action.pdf ebook
Grails in Action pdf
Getting-Started-with-Grails-Chinese,一本不错的grails入门书籍,希望对有志于grails的朋友们有用。
Grails in Action - Presentation pdf
Grails in Action Grails in Action Grails in Action Grails in Action Groovy in Action Grails in Action Grails in Action Groovy in Action Groovy in Action
Grails In Action 2009最新版,英文的。很详细,学习groovy和grails的不二之选。
ant-1.9.4jarant-antlr-1.9.4.jarant-junit-1.9.4jar ant-launcher-1.9.4.jar bsf-2.4.0.jar commons-cli-1.2jar commons-logging-1.2.jar gpars-1.2.1.jar groovy-2.4.5jar groovy-ant-2.4.5.jar ...
Grails入门指南中文pdf -- 针对grails1.0.4更新,附加idea8 开发grails的流程
基于java的动态语言Groovy 构建的敏捷web开发框架grails经典入门教程
wsdl2java源码 请注意,当前的 3.x 版本使用 cxf 3.0.4 并且需要 WSS4J ...grails-app\endpoints** 和 grails-app\services** 目录中自动装配配置的类 端点创建脚本 create-endpoint 和 create-endpoin
实测可用
实测可用
实测可用
实测可用