zoukankan      html  css  js  c++  java
  • Hibernate中createCriteria即QBC查询的详细用法

    现在假设有一个Student类,内有id,name,age属性
    String hql = "from Student s";
    按照以前的做法,我们通常是
    Query query = session.createQuery(hql);
    或者要按照条件检索的话.
    String hql = "from Student s where s.name like '王%'"
    Query query = session.createQuery(hql);
    不用HQL而使用QBC的话,那么代码为:
    Criteria criteria =session.createCriteria(Student.class);
    Criterion criterion = Expression.like("name","王%");
    这样还看不出来.那我们把检索条件加上年龄为22.
    HQL:
    String hql = "from Student s where s.name like '王%' and s.age= 22 ";
    Query query = session.createQuery(hql);
    List list = query.list();
    QBC:
    Criteria criteria =session.createCriteria(Student.class);
    Criterion criterion1 = Expression.like("name","王%");
    Criterion criterion2 = Expression.eq("age",newInteger(22));
    criteria.add(criterion1).add(criterion2);
    List list = criteria.list();


    看上去烦琐很多.但是做过项目的人都知道,当一个模块业务逻辑发生改变的时候,往往要重写sql,最烦也是最讨厌的就是拿着别人的hql或者sql,两眼瞪的溜园找到底要修改什么地方呢?
    如果使用QBC大大的增加了代码的可读性,以及可维护性.
    需要注意的是null
    比如我们要查找姓名为null的Student对象时应该这么写
    Criteria criteria =session.createCriteria(Student.class);
    Criterion criterion = Expression.isNull("name");
    criteria.add(criterion).list();
    以及使用between...and的时候
    Criteria criteria =session.createCriteria(Student.class);
    Criterion criterion1 = Expression.ge("age",new Integer(20));//下限
    Criterion criterion2 = Expression.le("age",new Integer(25));//上限
    //这里也可以把上述两个条件添加到第三个条件里
    Criterion criterion3 =Expression.and(criterion1,criterion2);
    criteria.add(criterion3).list();
    相当于from Student s where s.age between 20 and 25
    等同于from Student s where s.age >= 20 and s.age <=25


    下面是就HQL和QBC常用的查询条件做的比较
    表达式含义                                     HQL                                     QBC
    大于等于                                         >=                                        Expression.ge()
    大于                                                 >                                         Expression.gt()
    小于等于                                         <=                                        Expression.le()
    小于                                                 <                                         Expression.lt()
    等于                                                 =                                         Expression.eq()
    不等于                                             <>或者!=                            Expression.ne()    

    为空                                                 is null                             Expression.isNull()
    不为空                                             is notnull                        Expression.isNotNull()
    在指定范围内                                 betweenand                        Expression.between()
    不在指定范围                                 not betweenand                Expression.not(Expression.between())
    属于某个集合                                 in                                        Expression.in()
    不属于某个集合                             notin                                 Expression.not(Expression.in())
    与                                                    and                                     Expression.and()
    或                                                    or                                        Expression.or()
    非                                                    not                                     Expression.not()
    模糊查询                                        like                                    Expression.like
  • 相关阅读:
    2018.12.1 区块链论文翻译
    2018.11.29 区块链论文翻译
    jshell 一个代码片段测试工具(jdk9后新增的功能)
    java 的var 定义变量有点鸡肋...
    小心Math.abs(-2147483648)的坑
    java获取同级目录下的文件
    java获取formdata里的所有参数
    No enclosing instance of type VolatleTest is accessible. Must qualify the allocation with an enclosing instance of type VolatleTest
    if else太多怎么代替,太难维护?可以使用spring-plugin 插件系统
    设计一个泛型的获取数组最大值的函数.并且这个方法只能接受Number的子类并且实现了Comparable接口
  • 原文地址:https://www.cnblogs.com/Nbge/p/2767468.html
Copyright © 2011-2022 走看看