QBC常用限定方法
Restrictions
.eq
--> equal,等于.
Restrictions
.ne
--> not equal,不等于.
Restrictions
.allEq
--> 参数为Map对象,使用key/value进行多个等于的比对,相当于多个Restrictions
.eq 的效果
Restrictions
.gt
--> great-than > 大于
Restrictions
.ge
--> great-equal >= 大于等于
Restrictions
.lt
--> less-than, < 小于
Restrictions
.le
--> less-equal <= 小于等于
Restrictions
.between
--> 对应SQL的between子句
Restrictions
.like
--> 对应SQL的LIKE子句
Restrictions
.in
--> 对应SQL的in子句
Restrictions
.and
--> and 关系
Restrictions
.or
--> or 关系
Restrictions
.isNull
--> 判断属性是否为空,为空则返回true
Restrictions
.isNotNull
--> 与isNull相反
Restrictions
.sqlRestriction
--> SQL限定的查询
Order.asc
--> 根据传入的字段进行升序排序
Order.desc
--> 根据传入的字段进行降序排序
MatchMode.EXACT
--> 字符串精确匹配.相当于"like 'value'"
MatchMode.ANYWHERE
--> 字符串在中间匹配.相当于"like '%value%'"
MatchMode.START
--> 字符串在最前面的位置.相当于"like 'value%'"
MatchMode.END
--> 字符串在最后面的位置.相当于"like '%value'"
例子
查询年龄在20-30岁之间的所有学生对象
List list = session.createCriteria(Student.class)
.add(Restrictions
.between("age",new Integer(20),new Integer(30)).list();
查询学生姓名在AAA,BBB,CCC之间的学生对象
String[] names = {"AAA","BBB","CCC"};
List list = session.createCriteria(Student.class)
.add(Restrictions
.in("name",names)).list();
查询年龄为空的学生对象
List list = session.createCriteria(Student.class)
.add(Restrictions
.isNull("age")).list();
查询年龄等于20或者年龄为空的学生对象
List list = session.createCriteria(Student.class)
.add(Restrictions
.or(Restrictions
.eq("age",new Integer(20)),
Restrictions
.isNull("age")).list();
--------------------------------------------------------------------
使用QBC实现动态查询
public List findStudents(String name,int age){
Criteria criteria = session.createCriteria(Student.class);
if(name != null){
criteria.add(Restrictions
.liek("name",name,MatchMode.ANYWHERE));
}
if(age != 0){
criteria.add(Restrictions
.eq("age",new Integer(age)));
}
criteria.addOrder(Order.asc("name"));//根据名字升序排列
return criteria.list();
}
-----------------------------------------------------------------------------------
今天用了写hibernate
高级查询时用了Restrictions
(当然Expression也是可以以的)这个类.感觉不错.
下面的代码写的不易读.其实核心就是一句
Restrictions
.or(Restrictions
.like(),Restrictions
.or(Restrictions
.like,........))
里面的or可以无限加的.还是比较好用
Session session = getHibernateTemplate().getSessionFactory().openSession();
Criteria criteria = session.createCriteria(Film.class);
List<Film> list = criteria.add(
Restrictions
.or(Restrictions
.like("description", key,MatchMode.ANYWHERE),
Restrictions
.or(Restrictions
.like("name", key,MatchMode.ANYWHERE),
Restrictions
.or(Restrictions
.like("direct", key,MatchMode.ANYWHERE),
Restrictions
.or(Restrictions
.like("mainplay",key,MatchMode.ANYWHERE),
Restrictions
.like("filearea", key,MatchMode.ANYWHERE)))))).list();
session.close();
return list;
分享到:
相关推荐
。。。
。。。
Dynamic IP Restrictions: 保護 IIS 網站的流量守門員 iis 插件
To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain ...
使用和模板通过引导了该项目。 国家应用测试 开始: npm install npm start 一些常见问题解答: 1)为什么使用Redux工具包: saves time and have almost all the necessary package from scratch for test ...
PDF文档有时不能复制,粘贴,可能是因为屏蔽了复制功能,A-PDF Restrictions Remover可以解除这种屏蔽
Hibernate Object/Grid Mapper (OGM) aims at providing Java Persistence (JPA) support for NoSQL solutions. It reuses Hibernate Core's engine but persists entities into a NoSQL data store instead of a ...
Restrictions方法分页方法一些集合
A-pdf Restrictions Remover 是一款破解PDF密码限制的软件 这个事A-pdf Restrictions Remover 的破解版。。。 真绕口 把补丁rar解压到其他文件一起,然后用loadX或loadA,loadT(因系统而定)
"IP and domain restrictions" 是IIS提供的一个特性,提供了黑白名单,来限制用户对web资源的访问。由于没有合适的对比"IP and domain restriction" filtering list 与客户外部请求,导致攻击者可以构造特定的请求...
jar包:项目中除了使用了struts2,hibernate3.0和spring1.8以外(用spring2.0的包也可以.不能低于1.8的包.)还是用了junit,ajax,第三方的table组件.等等.所以需要下载相对应的包. 为了上传jar.我专门申请了一个网盘.所有...
注:Hibernate 有冬眠之意,Squirrel ( 松鼠 ) 具有冬眠的习性,项目中众多类名称沿袭自 Hibernate,如 Criteria、Criterion、Restrictions 等。 Query private static Session session; @BeforeClass public static...
7-Zip压缩软件7-Zip is open source software. Most of the source code is under the ... The unRAR code is under a mixed license: GNU LGPL + unRAR restrictions. Check license information here: 7-Zip license.
NULL 博文链接:https://caohong286.iteye.com/blog/1332905
它可以使用不限数量的线程来完成此过程。 安装 apt包即将推出 从源编译 确保在计算机上安装了gcc(4.7或更高版本),make,openssl和openssl-dev(仅Linux,macOS请参见 。此问题将在以后的版本中修复) sudo apt ...
Quantum Complexity restrictions on algorithms and architectures 【PhD 】
Example em=Example.create(student).setPropertySelector(new EmptyStringSelector()); List list =session.createCriteria(Student.class).add... .add(Restrictions.between("birthday", date1, date2)) .list();
jdbc: 1.数据库连接的方式: ODBC:开放式数据库连接。 C语言实现的,提供语言和(基于SQL的)数据库进行交互的“一致性”的...1.load()方法默认要加载的对象一定存在,所以很放心的使用代理对象,等到用到的时候从...
去除PDF 文件不让打印