【转】Hibernate HQL查询 分页查询 模糊查询
- /**
- * HQL查询的一个例子
- */
- public static void hql()
- {
- Session s = null;
- try
- {
- s = HibernateUtil.getSeesion();
- //final String hql = "from User as u where u.name=?";
- final String hql = "from User as u where u.name=:name";
- final Query query = s.createQuery(hql);
- //query.setString(0, "北京市"); //从0开始
- query.setString("name", "北京市");
- final List<User> list = query.list();
- for (final User u : list)
- {
- System.out.println(u.getName());
- }
- }
- finally
- {
- if (s != null)
- {
- s.close();
- }
- }
- System.out.println("HQL完成");
- }
HibernateUtil工具类
- package dao;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- /**
- * 这是一个工具类, 快速取得session
- *
- */
- public class HibernateUtil
- {
- static SessionFactory sessionFactory = null;
- static
- {
- final Configuration cfg = new Configuration();
- cfg.configure(); //路径可以改变
- sessionFactory = cfg.buildSessionFactory();
- }
- public static SessionFactory getSessionFactory()
- {
- return sessionFactory;
- }
- /**
- * 取得session
- *
- * @return session
- */
- public static Session getSeesion()
- {
- return sessionFactory.openSession();
- }
- }
- /**
- * HQL 分页查询
- */
- public static void page()
- {
- Session s = null;
- try
- {
- s = HibernateUtil.getSeesion();
- final String hql = " from User "; //User是类名
- final Query q = s.createQuery(hql);
- q.setFirstResult(0); //从第0条开始
- q.setMaxResults(10); //取出10条
- final List list = q.list();
- for (final User u : list)
- {
- System.out.println(u.getId() + " " + u.getName());
- }
- }
- finally
- {
- s.clear();
- }
- }
模糊查询
- public List<User> getUsers(String id){
- List list=new ArrayList<User>();
- String hql="from User as user where user.id like :id"; //参数名称查询
- factory=DBHelper.getSessionFactory();
- Session session=factory.openSession();
- Transaction transaction=session.beginTransaction();
- Query query=session.createQuery(hql);
- query.setString("id", "%"+id+"%");
- list=query.list();
- transaction.commit();
- session.close();
- return list;
- }
- //可以拼字符串
- List students = session.createQuery("select s.id, s.name from Student s where s.name like '%1%'").list();
- //Query query = session.createQuery("select s.id, s.name from Student s where s.name like ?");
- //query.setParameter(0, "%1%");
- //List students = query.list();
- //可以使用?方式传递参数
- //参数的索引从0开始
- //传递的参数值,不用单引号引起来
- //注意方法链编程
- List students = session.createQuery("select s.id, s.name from Student s where s.name like ?")
- .setParameter(0, "%1%")
- .list();
- //使用 :参数名称 的方式传递参数值
- List students = session.createQuery("select s.id, s.name from Student s where s.name like :myname")
- .setParameter("myname", "%1%")
- .list();
- //使用 :参数名称 的方式传递参数值
- List students = session.createQuery("select s.id, s.name from Student s where s.name like :myname and s.id=:myid")
- .setParameter("myname", "%1%")
- .setParameter("myid", 12)
- .list();
- //支持in,需要使用setParameterList进行参数传递
- List students = session.createQuery("select s.id, s.name from Student s where s.id in(:myids)")
- .setParameterList("myids", new Object[]{1, 2, 3, 4, 5})
- .list();
- //查询2008年2月创建的学生
- List students = session.createQuery("select s.id, s.name from Student s where date_format(s.createTime, '%Y-%m')=?")
- .setParameter(0, "2008-02")
- .list();
- //查询2008-01-10到2008-02-15创建的学生
- List students = session.createQuery("select s.id, s.name from Student s where s.createTime between ? and ?")
- .setParameter(0, sdf.parse("2008-01-10 00:00:00"))
- .setParameter(1, sdf.parse("2008-02-15 23:59:59"))
- .list();
转载自http://blog.csdn.net/heardy/article/details/6100106