网站首页 > 技术文章 正文
使用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()
- 上一篇: 举个例子,说说用SQL怎么处理文本数据
- 下一篇: Linux命令-nl命令 linux命令nano
猜你喜欢
- 2024-11-03 《MySQL 入门教程》第15篇MySQL常用函数之字符函数
- 2024-11-03 关于Oracle和PostgreSQL中非正常日期提取并转换
- 2024-11-03 分享一个有意思的渗透测试工具——sqlmap
- 2024-11-03 黑客最喜欢的脚本之一Perl,让你拥有无限可能
- 2024-11-03 「NLP2005年以来大突破」语义角色标记深度模型,准确率提升10%
- 2024-11-03 自生成数据实现LLM自我纠正,DeepMind新突破:纠正性能提升15.9%
- 2024-11-03 超越99.9%人类玩家,微软专业十段麻将AI论文细节首次公布
- 2024-11-03 如何避免出现 SQL 注入漏洞 如何避免 sql 注入?
- 2024-11-03 Linux命令-nl命令 linux命令nano
- 2024-11-03 举个例子,说说用SQL怎么处理文本数据
你 发表评论:
欢迎- 623℃几个Oracle空值处理函数 oracle处理null值的函数
- 615℃Oracle分析函数之Lag和Lead()使用
- 603℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 599℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 595℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 588℃【数据统计分析】详解Oracle分组函数之CUBE
- 576℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 565℃Oracle有哪些常见的函数? oracle中常用的函数
- 最近发表
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- 前端获取当前时间 (50)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (80)
- oracle 工具 (55)
- oracle 内存 (55)
- oracle 导出表 (62)
- oracle约束 (54)
- oracle 中文 (51)
- oracle链接 (54)
- oracle的函数 (58)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)