HQL运算符 |
QBC运算符 |
含义 |
= |
Restrictions.eq() |
等于equal |
<> |
Restrictions.ne() |
不等于not equal |
> |
Restrictions.gt() |
大于greater than |
>= |
Restrictions.ge() |
大于等于greater than or equal |
< |
Restrictions.lt() |
小于less than |
<= |
Restrictions.le() |
小于等于less than or equal |
is null |
Restrictions.isnull() |
等于空值 |
is not null |
Restrictions.isNotNull() |
非空值 |
like |
Restrictions.like() |
字符串模式匹配 |
and |
Restrictions.and() |
逻辑与 |
and |
Restrictions.conjunction() |
逻辑与 |
or |
Restrictions.or() |
逻辑或 |
or |
Restrictions.disjunction() |
逻辑或 |
not |
Restrictions.not() |
逻辑非 |
in(列表) |
Restrictions.in() |
等于列表中的某一个值 |
not in(列表) |
Restrictions.not(Restrictions.in()) |
不等于列表中任意一个值 |
between x and y |
Restrictions.between() |
闭区间xy中的任意值 |
not between x and y |
Restrictions.not(Restrictions..between()) |
小于值X或者大于值y
|
2,QBC常用限定方法
Restrictions.eq --> 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();
}
public List findByCriteria(Object[] args) {
return HibernateSessionFactory
.getSession()
.createCriteria(Emp.class)
// and
.add(Restrictions.eq("id", 123))
.add(Restrictions.gt("hriedate", "1985-4-1"))
.add(Restrictions.ge("hriedate", "1985-4-1"))
.add(Restrictions.lt("hriedate", "1985-4-1"))
.add(Restrictions.le("hriedate", "1985-4-1"))
// or
.add(Restrictions.or(Restrictions.eq("id", 123),
Restrictions.gt("hriedate", "1985-4-1")))
// 多个or
.add(Restrictions.disjunction().add(Restrictions.eq("id", 123))
.add(Restrictions.gt("hriedate", "1985-4-1"))
.add(Restrictions.ge("hriedate", "1985-4-1")))
// 各种条件
.add(Restrictions.between("age", "19", "25"))
// 张(STRAT是张在开头(END是张在后头)(ANYWHERE任何位置)(EXACT严格匹配))%
.add(Restrictions.like("name", "zhang", MatchMode.START))
.add(Restrictions.ilike("name", "zhang"))
// 忽略大小写
.add(Restrictions.in("id", args))
.add(Restrictions.isEmpty("emps"))
// Dept.emps(针对集合) 没有员工的部门
.add(Restrictions.isNotEmpty("emps"))
.add(Restrictions.not(Restrictions.isEmpty("emps")))// 给某些不带一对的条件取反用的
.add(Restrictions.isNull("sal"))// sal is null
.add(Restrictions.isNotNull("sal"))// sal is not null
.add(Restrictions.sizeEq("emps", 5))// 查询部门人数等于 5个的
.add(Restrictions.eq("id", 5)).add(Restrictions.ne("id", 5))// 不等于5
.add(Restrictions.eqProperty("birthday", "hiredate"))// 两个属性相等,其他属性也一样F
.list();
}
}