优秀的编程知识分享平台

网站首页 > 技术文章 正文

Grails指南22查询基础(grails入门指南)

nanyue 2024-09-09 04:58:33 技术文章 7 ℃

使用list()方法获取给定类的所有实例

def books = Book.list()

list()方法支持依据参数来进行分页

def books = Book.list(offset:10, max:20) //offset数据起始位置,max最大数量

以及排序(注:title是Book模型Domain中的属性名)

def books = Book.list(sort:"title", order:"asc") //sort以什么进行排序,order次序的asc升序还是desc降序

#####################

根据数据库id获取数据,使用get(id)方法:

def book = Book.get(23)

也可获得一组数据,术语getAll()方法:

def books = Book.getAll([23, 93, 81])

def books = Book.getAll(23, 93, 81)

#####################

GORM支持动态查找器的概念,动态查找器的使用看上去像是对静态方法的调用,但实际上这些方法本身在代码中并不存在,而是在程序运行时基于模型Domain的属性自动生成的,举个例子:

class Book {

String title

Date releaseDate

Author author

}

class Author {

String name

}

使用方法(findBy*与findAllBy*):

def book = Book.findByTitle("The Stand")

book = Book.findByTitleLike("Harry Pot%")

book = Book.findByReleaseDateBetween(firstDate, secondDate)

book = Book.findByReleaseDateGreaterThan(someDate)

book = Book.findByTitleLikeOrReleaseDateLessThan("%Something%", someDate)

################

InList - 数据在列表中

LessThan - 数据小于给定值

LessThanEquals - 数据小于等于给定值

GreaterThan - 数据大于给定值

GreaterThanEquals - 数据大于等于给定值

Like - 模糊查询(模糊匹配),数据类似于给定值

Ilike - 模糊查询,数据类似于给定值,忽略大小写

NotEqual - 数据不等于给定值

InRange - 数据在给定范围内,例new IntRange(1, 3)

Rlike - 在使用MySQL或Oracle数据库时,Like中支持正则表达式的写法;使用其它数据库时,不支持正则,仅支持普通模糊检索,用法如Like

Between - 数据在两个值之间(需要两个参数)

IsNotNull - 数据不为null(不需要参数)

IsNull - 数据为null(不需要参数)

################

使用举例1:

def now = new Date()

def lastWeek = now - 7

def book = Book.findByReleaseDateBetween(lastWeek, now)

books = Book.findAllByReleaseDateIsNull()

books = Book.findAllByReleaseDateIsNotNull()

使用举例2:

def books = Book.findAllByTitleLikeAndReleaseDateGreaterThan("%Java%", new Date() - 30)

def books = Book.findAllByTitleLikeOrReleaseDateGreaterThan("%Java%", new Date() - 30)

使用举例3:

def author = Author.findByName("Stephen King")

def books = author? Book.findAllByAuthor(author) : []

分页与排序:

def books = Book.findAllByTitleLike("Harry Pot%", [max: 3, offset: 2, sort: "title", order: "desc"])

################

where查询,基于DetachedCriteria,比动态查找器更灵活,比Criteria更方便,支持组合查询。

def query = Person.where {

firstName == "Bart"

} //DetachedCriteria,组合查询的关键

Person bart = query.find() //开始查询

################

立即查询的方法举例:

def results = Person.findAll {

lastName == "Simpson"

}

def results = Person.findAll(sort:"firstName") {

lastName == "Simpson"

}

Person p = Person.find { firstName == "Bart" }

################

操作符,Criteria方法,描述

==,eq,数据等于给定值

!=,ne,数据不等于给定值

>,gt,数据大于给定值

<,lt,数据小于给定值

>=,ge,数据大于等于给定值

<=,le,数据小于等于给定值

in,inList,数据在列表中

==~,like,模糊查询,数据类似于给定值

=~,ilike,模糊查询,数据类似于给定值,忽略大小写

################

def query = Person.where {

(lastName != "Simpson" && firstName != "Fred") || (firstName == "Bart" && age > 9)

}

def results = query.list(sort:"firstName")

正则表达式的写法(数据库需支持,如MySQL或Oracle):

def query = Person.where {

firstName ==~ ~/B.+/

}.list() //firstName需满足模式“大小写b加任意的非\n的字符至少一个”

注:

==~代表like,即模糊查询

~/正则表达式/,正则的标准写法

################

其他例子:

def query = Person.where {

age in 18..65

}

def query = Person.where {

middleName == null

}

################

查询组合:

DetachedCriteria<Person> query = Person.where {

lastName == "Simpson"

}

DetachedCriteria<Person> bartQuery = query.where {

firstName == "Bart"

}

Person p = bartQuery.find()

附where的另一种写法:

import grails.gorm.DetachedCriteria

def callable = {

lastName == "Simpson"

} as DetachedCriteria<Person>

def query = Person.where(callable)

附且&&与或||的用法:

def query = Person.where {

(lastName != "Simpson" && firstName != "Fred") || (firstName == "Bart" && age > 9)

}

def query = Person.where {

firstName == "Fred" && !(lastName == 'Simpson')

}

################

关联查询:

def query = Pet.where {

owner.firstName == "Joe" || owner.firstName == "Fred" //owner是模型Pet的一个属性,也是Person模型的一个实例

}

def query = Person.where {

pets { name == "Jack" || name == "Joe" } //pets是模型Person的一个属性,也是Pet模型的实例集合

}

def query = Person.where {

pets { name == "Jack" } || firstName == "Ed"

}

def query = Person.where {

pets.size() == 2

}

以关联排序:

def query = Pet.where {

def o1 = owner

o1.firstName == "Fred"

}.list(sort:'o1.lastName')

################

子查询举例:

def query = Person.where {

age > avg(age)

}

方法,描述

avg,求平均值

sum,求和

max,最大值

min,最小值

count,求记录总数

property,在结果中过滤

def query = Person.where {

age > avg(age).of { lastName == "Simpson" } && firstName == "Homer"

}//先检索lastName是Simpson的所有记录,求年龄平均值。再检索firstName是Homer且年龄大于平均值的记录

Person.where {

age < property(age).of { lastName == "Simpson" }

} //先检索lastName是Simpson的所有记录,获得年龄集合。在检索所有满足年龄大于年龄集合中所有年龄的记录

################

更多的例子:

def results = Person.where {

firstName in where { age < 18 }.firstName

}.list()

def results = Person.withCriteria {

notIn "firstName", Person.where { age < 18 }.firstName

}

def results = Person.where {

age > where { age > 18 }.avg('age')

}

################

方法,描述

second,获取日期格式中的秒

minute,获取日期格式中的分钟

hour,获取日期格式中的小时

day,获取日期格式中的日

month,获取日期格式中的月份

year,获取日期格式中的年份

lower,将字符串转化为小写

upper,将字符串转化为大写

length,求字符串长度

trim,去除字符串前后空白

################

def query = Pet.where {

year(birthDate) == 2011

}

def query = Person.where {

year(pets.birthDate) == 2009

}

################

批量更新与批量删除:

DetachedCriteria<Person> query = Person.where {

lastName == 'Simpson'

}

int total = query.updateAll(lastName:"Bloggs")

DetachedCriteria<Person> query = Person.where {

lastName == 'Simpson'

}

int total = query.deleteAll()

Tags:

最近发表
标签列表