Hibernate QBC学习举例

阅读更多
public void testQBC() {    
            
            
//      方法  说明     
//      Restrictions.eq     =     
//      Restrictions.allEq  利用Map来进行多个等于的限制     
//      Restrictions.gt     >     
//      Restrictions.ge     >=     
//      Restrictions.lt     <    
//      Restrictions.le     <=     
//      Restrictions.between    BETWEEN    
//      Restrictions.like   LIKE    
//      Restrictions.in     in    
//      Restrictions.and    and    
//      Restrictions.or     or    
//      Restrictions.sqlRestriction     用SQL限定查询     
//介绍以下几个典型的用法,这样其他的也会用了         
            
            
            
       /*   
         * 简单的加些限制条件进行搜索    
         * Restrictions.eq   
         */   
        /* Session session = factory.openSession();   
         Criteria criteria = session.createCriteria(User.class);   
         Criterion criterion1 = Restrictions.eq("addr", "dalian");   
         Criterion criterion2 = Restrictions.eq("name", "tie");   
         criteria.add(criterion1);   
         criteria.add(criterion2);   
         List users = criteria.list();   
        for(User user : users){   
         System.out.println(user.introduce());   
         System.out.println("**********************");   
         }*/   
   
           
        /*   
         * Restrictions.allEq   利用Map来进行多个等于的限制    
         */   
        /* Session session = factory.openSession();   
         Criteria criteria = session.createCriteria(User.class);   
         Map para = new HashMap();   
         para.put("addr", "dalian");   
         para.put("name", "tie");   
         Criterion criterion1 = Restrictions.allEq(para);   
         criteria.add(criterion1);   
         List users = criteria.list();   
         for(User user : users){   
         System.out.println(user.introduce());   
         System.out.println("**********************");   
         }*/   
   
            
            
        /*   
         * Restrictions.sqlRestriction用SQL限定查询    
         * Restrictions.sqlRestriction()这里有三个重载的方法,   
         */   
    /*   Session session = factory.openSession();   
         Criteria criteria = session.createCriteria(User.class);   
//1.     Criterion criterion1 =   
         Restrictions.sqlRestriction("{alias}.name='tie' and   
         {alias}.addr='dalian'");   
  
//2.     Criterion criterion1 = Restrictions.sqlRestriction("alias}.name=?",   
         "tie", Hibernate.STRING);   
           
            
//3.     Object[] ob = {"tie","dalian"};   
         Type[] type = {Hibernate.STRING,Hibernate.STRING};   
  
         Criterion criterion1 = Restrictions.sqlRestriction("{alias}.name=?   
         and {alias}.addr=?", ob, type);   
         criteria.add(criterion1);   
         List users = criteria.list();   
         for(User user : users){   
         System.out.println(user.introduce());   
         System.out.println("**********************");   
         }*/   
   
            
        /*   
         * 排序   
         */   
        /* Session session = factory.openSession();   
         Criteria criteria = session.createCriteria(User.class);   
         criteria.addOrder(Order.desc("age"));   
         List users = criteria.list();   
         for(User user : users){   
         System.out.println(user.introduce());   
         System.out.println("**********************");   
         }*/   
   
            
        /*   
         * 求平均数   
         */   
        /* Session session = factory.openSession();   
         Criteria criteria = session.createCriteria(User.class);   
         criteria.setProjection(Projections.avg("age"));   
         List avg_ages = criteria.list();   
         for(Double avg_age : avg_ages){   
         System.out.println("avg_age:"+avg_age);   
         }*/   
   
            
            
        /*   
         * 分组   
         */   
        /* Session session = factory.openSession();   
         Criteria criteria = session.createCriteria(User.class);   
         criteria.setProjection(Projections.groupProperty("addr"));   
         List addrs = criteria.list();   
         for(String addr : addrs){   
         System.out.println(addr);   
         }*/   
   
            
            
        /*   
         *过个的条件,同时求平均数和分组    
         */   
        /* Session session = factory.openSession();   
         Criteria criteria = session.createCriteria(User.class);   
         ProjectionList projectionList = Projections.projectionList();   
         projectionList.add(Projections.groupProperty("addr"));   
         projectionList.add(Projections.avg("age"));   
         criteria.setProjection(projectionList);   
         List avgs = criteria.list();   
         for(Object[] avg : avgs){   
         System.out.println(avg[0]+":"+avg[1]);   
         }*/   
   
           
        /*   
         * DetachedCriteria举例   
         *    
         */   
            
    /*  DetachedCriteria detachedCriteria = DetachedCriteria   
                .forClass(User.class);   
        detachedCriteria.add(Restrictions.eq("addr", "dalian"));   
  
        Session session = factory.openSession();   
        Criteria criteria = detachedCriteria.getExecutableCriteria(session);   
  
        List users = criteria.list();   
        for (User user : users) {   
            System.out.println(user.introduce());   
            System.out.println("**********************");   
        }*/   
   
      //  session.close();    
    }   


本文转自 http://linweihan.iteye.com/blog/145190

你可能感兴趣的:(Hibernate,SQL,Blog)