论坛首页 入门技术论坛

hibernate常用语句

浏览 2411 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-05-21  

----------------------------------------------------------------------
HQL(Hibernate Query Language)是面向对象的查询语言

1 按照位置绑定

  Query query = session.createQuery("from Customer c where c.username = ?");
  query.setString(0, "name1");
  Customer customer  = (Customer)query.uniqueResult();
  //Customer customer  = (Customer)query.setMaxResults(1).uniqueResult();
  System.out.println(customer.getUsername());
 
2
  Iterator<Users> it= session.createQuery(" FROM Users WHERE username=? AND age=?").setString(0, "hujin").setInteger(1, 25).list().iterator();
  while(it.hasNext())
{
Users user = it.next();
System.out.println("用户:"+user.getUsername()+"  密码:"+user.getPassword());
}
3
   List result=session.createQuery("……").setString("customerName","Tom").setInteger("customerAge",21).list();
  

----------------------------------------------------------------------  

QBC检索方式

1   //创建一个Criteria对象
     Criteria criteria=session.createCriteria(Customer.class);
    
     //设定查询条件,然后把查询条件加入到Criteria中
     Criterion criterion1= Expression.like("name", "T%") ;
     Criterion criterion2= Expression.eq("age", new Integer(21)) ;
    
     criteria=criteria.add(criterion1);
     criteria=criteria.add(criterion2);
    
     //执行查询语句,返回查询结果
     List result=criteria.list();
2   
    List result=session.createCriteria(Customer.class) .add(Expression.like("name", "T%").add(Expression.eq("age", new Integer(21)).list();

    
    
3  Criteria crit = sess.createCriteria(Cat.class);              
    crit.setMaxResults(50);
    List cats = crit.list();   




---------------------------------------------------------------------
        对于集合遍历的问题
       
        1 如果集合中是对象,可以如下打印出来,如:
         
          Query query = (Query)session.createQuery("from Customer");
          List list = query.list();
         
          for (int i = 0; i < list.size(); i++)
          {
             Customer c = (Customer)list.get(i);
             System.out.println(c.getId()+"  "+c.getUsername());
          }

        2 如果集合中为数组,可以如下显示出来:
        
          Query query = (Query)session.createQuery("select id,username from Customer");
          List list = query.list();
        
          for (int i = 0; i < list.size(); i++)
            {
                Object[] o = (Object[])list.get(i);
                System.out.println(o[0]+"   "+o[1]);
            }


    
    
----------------------------------------------------------------------
    
     分页查询
    
     1 采用HQL检索方式
    
      Query query = session.createQuery("from Customer c order by c.name asc");
      query.setFirstResult(0);
      query.setMaxResults(10);
      List result = query.list();
     
     2 采用QBC检索方式
    
     Criteria criteria = session.createCriteria(Customer.class);
     criteria.addOrder(Order.asc("name"));
     criteria.setFirstResult(0);
     criteria.setMaxResults(10);
     List result = criteria.list();

       

     
     
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics