网站首页 > 技术文章 正文
使用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()
猜你喜欢
- 2024-09-09 混合云资产管理项目(二)(混合云存储产品有哪些)
- 2024-09-09 Go语言进阶之Go语言高性能Web框架Iris项目实战-完善用户管理EP04
- 2024-09-09 数据库与 Go 的交互(go数据库和kegg数据库)
- 2024-09-09 七爪源码:N+1 查询如何烧毁您的数据库
- 2024-09-09 Go的安全编程和防御性编程(防止代码注入)
- 2024-09-09 Vue3+Go 仿抖音项目架构设计与实现
- 2024-09-09 腾讯Go安全指南(腾讯官网最新安全公告)
- 2024-09-09 Grails指南24查询高阶(grails中文参考手册)
- 2024-09-09 Redis优化高并发下的秒杀性能(redis秒杀高并发代码)
- 2024-09-09 10.Go语言编写个人博客 文章分类(基于golang的个人博客系统)
- 02-21走进git时代, 你该怎么玩?_gits
- 02-21GitHub是什么?它可不仅仅是云中的Git版本控制器
- 02-21Git常用操作总结_git基本用法
- 02-21为什么互联网巨头使用Git而放弃SVN?(含核心命令与原理)
- 02-21Git 高级用法,喜欢就拿去用_git基本用法
- 02-21Git常用命令和Git团队使用规范指南
- 02-21总结几个常用的Git命令的使用方法
- 02-21Git工作原理和常用指令_git原理详解
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)