QBC(Query By Criteria) QBE (Query By Example)

QBC  参考:(Hibernate的QBC查询)

    //is empty and is not empty
    @Test
    public void testQBC(){
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        //criterion 标准/准则/规范
        Criteria c = session.createCriteria(Topic.class)//from Topic
                .add(Restrictions.gt("id", 2))    //greater than = id > 2
                .add(Restrictions.lt("id", 8))    //less than = id < 8
                .add(Restrictions.like("title", "t_"))
                .createCriteria("category")
                .add(Restrictions.between("id", 0, 5)) //category.id >= 0 and category.id <= 5
                ;
        //DetachedCriteria
        
        for(Object o : c.list()){
            Topic t = (Topic)o;
            System.out.println(t.getId() + "-" + t.getTitle() );
        }
        session.getTransaction().commit();
    }

QBE   参考:(hibernate的QBE和QBC) 和 (Hibernate 查询方式(HQL/QBC/QBE)汇总)

 1     //is empty and is not empty
 2     //QBC Query By Criteria
 3     //QBE Query By Example
 4     @Test
 5     public void testQBE(){
 6         Session session = sf.getCurrentSession();
 7         session.beginTransaction();
 8         
 9         Topic tExample = new Topic();
10         tExample.setTitle("T_");
11         Example e = Example.create(tExample)
12                 .ignoreCase().enableLike();//构建 example
13         
14         Criteria c = session.createCriteria(Topic.class)
15                 .add(Restrictions.gt("id", 3))
16                 .add(Restrictions.lt("id", 8))
17                 .add(e)
18                 ;
19         
20         for(Object o : c.list()){
21             Topic t = (Topic)o;
22             System.out.println(t.getId() + "-" + t.getTitle() );
23         }
24         session.getTransaction().commit();
25     }

jar包链接: https://pan.baidu.com/s/1kVbVPmZ 密码: 384f

QBC链接: https://pan.baidu.com/s/1dFEHIYt 密码: nfms

QBE链接: https://pan.baidu.com/s/1i4JJa0d 密码: 2zby

posted @ 2017-04-26 11:09  流年如水烟雨随风  阅读(1297)  评论(0编辑  收藏  举报