【转】Hibernate HQL查询 分页查询 模糊查询

 
 
  1. /**     
  2.   * HQL查询的一个例子     
  3. */       
  4.  public static void hql()        
  5.  {        
  6.      Session s = null;        
  7.     
  8.      try       
  9.      {        
  10.          s = HibernateUtil.getSeesion();        
  11.          //final String hql = "from User as u where u.name=?";        
  12.          final String hql = "from User as u where u.name=:name";        
  13.          final Query query = s.createQuery(hql);        
  14.          //query.setString(0, "北京市");  //从0开始        
  15.         query.setString("name""北京市");        
  16.          final List<User> list = query.list();        
  17.          for (final User u : list)        
  18.          {        
  19.              System.out.println(u.getName());        
  20.          }        
  21.      }        
  22.      finally       
  23.      {        
  24.          if (s != null)        
  25.          {        
  26.              s.close();        
  27.          }        
  28.      }        
  29.      System.out.println("HQL完成");        
  30.  }      

HibernateUtil工具类

  1. package dao;        
  2.        
  3. import org.hibernate.Session;        
  4. import org.hibernate.SessionFactory;        
  5. import org.hibernate.cfg.Configuration;        
  6.        
  7. /**     
  8.  * 这是一个工具类, 快速取得session     
  9.  *      
  10.  */       
  11. public class HibernateUtil        
  12. {        
  13.     static SessionFactory   sessionFactory  = null;        
  14.     static       
  15.     {        
  16.         final Configuration cfg = new Configuration();        
  17.         cfg.configure(); //路径可以改变        
  18.         sessionFactory = cfg.buildSessionFactory();        
  19.     }        
  20.        
  21.     public static SessionFactory getSessionFactory()        
  22.     {        
  23.         return sessionFactory;        
  24.     }        
  25.        
  26.     /**     
  27.      * 取得session     
  28.      *      
  29.      * @return session     
  30.      */       
  31.     public static Session getSeesion()        
  32.     {        
  33.         return sessionFactory.openSession();        
  34.     }        
  35. }      
  1. /**    
  2. * HQL 分页查询    
  3. */      
  4. public static void page()      
  5. {      
  6.     Session s = null;      
  7.     try      
  8.     {      
  9.         s = HibernateUtil.getSeesion();      
  10.         final String hql = " from User "//User是类名      
  11.         final Query q = s.createQuery(hql);      
  12.         q.setFirstResult(0); //从第0条开始      
  13.         q.setMaxResults(10); //取出10条      
  14.         final List list = q.list();      
  15.         for (final User u : list)      
  16.         {      
  17.             System.out.println(u.getId() + " " + u.getName());      
  18.         }      
  19.     
  20.     }      
  21.     finally      
  22.     {      
  23.         s.clear();      
  24.     }      
  25. }    

模糊查询

  1. public List<User> getUsers(String id){     
  2.     List list=new ArrayList<User>();     
  3.     String hql="from User as user where user.id like :id"//参数名称查询  
  4.     factory=DBHelper.getSessionFactory();     
  5.     Session session=factory.openSession();     
  6.     Transaction transaction=session.beginTransaction();     
  7.     Query query=session.createQuery(hql);     
  8.      query.setString("id""%"+id+"%");         
  9.     list=query.list();     
  10.     transaction.commit();     
  11.     session.close();     
  12.     return list;     
  13. }  
    1. //可以拼字符串     
    2. List students = session.createQuery("select s.id, s.name from Student s where s.name like '%1%'").list();     
    3.     
    4. //Query query = session.createQuery("select s.id, s.name from Student s where s.name like ?");     
    5. //query.setParameter(0, "%1%");     
    6. //List students = query.list();     
    7.                  
    8. //可以使用?方式传递参数     
    9. //参数的索引从0开始     
    10. //传递的参数值,不用单引号引起来     
    11. //注意方法链编程     
    12. List students = session.createQuery("select s.id, s.name from Student s where s.name like ?")     
    13.             .setParameter(0"%1%")     
    14.             .list();     
    15.     
    16. //使用 :参数名称 的方式传递参数值     
    17. List students = session.createQuery("select s.id, s.name from Student s where s.name like :myname")     
    18.                .setParameter("myname""%1%")     
    19.                .list();     
    20.     
    21. //使用 :参数名称 的方式传递参数值     
    22. List students = session.createQuery("select s.id, s.name from Student s where s.name like :myname and s.id=:myid")     
    23.                .setParameter("myname""%1%")     
    24.                .setParameter("myid"12)     
    25.                .list();     
    26.     
    27. //支持in,需要使用setParameterList进行参数传递     
    28. List students = session.createQuery("select s.id, s.name from Student s where s.id in(:myids)")     
    29.             .setParameterList("myids"new Object[]{12345})     
    30.             .list();     
    31.                  
    32. //查询2008年2月创建的学生     
    33. List students = session.createQuery("select s.id, s.name from Student s where date_format(s.createTime, '%Y-%m')=?")     
    34.             .setParameter(0"2008-02")     
    35.             .list();     
    36.     
    37. //查询2008-01-10到2008-02-15创建的学生     
    38. List students = session.createQuery("select s.id, s.name from Student s where s.createTime between ? and ?")     
    39.             .setParameter(0, sdf.parse("2008-01-10 00:00:00"))     
    40.             .setParameter(1, sdf.parse("2008-02-15 23:59:59"))     
    41.             .list(); 

转载自http://blog.csdn.net/heardy/article/details/6100106

原文地址:https://www.cnblogs.com/luhuimin/p/3342510.html