session.beginTransaction();Stringhql="from User";// from 后跟的是要查询的对象,而不是表Queryquery=session.createQuery(hql);List<User>userList=query.list();for(Useruser:userList){System.out.println(user.getUserName());}session.getTransaction().commit();
带where的查询示例
1234567891011121314151617181920
session.beginTransaction();Stringhql="from User where userName = 'James'";Queryquery=session.createQuery(hql);List<User>userList=query.list();for(Useruser:userList){System.out.println(user.getUserName());}session.getTransaction().commit();/* 在HQL中where语句中使用的是持久化对象的属性名,如上面示例中的userName。当然在HQL中也可以使用别名*/Stringhql="from User as u where u.userName = 'James'";/*过滤条件 在where语句中还可以使用各种过滤条件,如:=、<>、<、>、>=、<=、between、not between、in、not in、is、like、and、or等*/
获取一个不完整的对象
12345678910111213141516171819202122
session.beginTransaction();Stringhql="select userName from User";Queryquery=session.createQuery(hql);List<Object>nameList=query.list();for(Objectobj:nameList){ String name=(String)obj; System.out.println(name);}session.getTransaction().commit();// 多个属性的话,需要用object[]接收session.beginTransaction();Stringhql="select userName,userPwd from User";Queryquery=session.createQuery(hql);ListnameList=query.list();for(Objectobj:nameList){Object[]array=(Object[])obj;// 转成object[]System.out.println("name:"+array[0]);System.out.println("pwd:"+array[1]);}session.getTransaction().commit();
统计和分组查询
12345678910111213141516171819
session.beginTransaction();Stringhql="select count(*),max(id) from User";Queryquery=session.createQuery(hql);ListnameList=query.list();for(Objectobj:nameList){Object[]array=(Object[])obj;System.out.println("count:"+array[0]);System.out.println("max:"+array[1]);}session.getTransaction().commit();/* 该条sql语句返回的是单条数据,所以还可以这样写 单列数据用Object,多列数据用Object[]*/Object[]object=(Object[])query.uniqueResult();System.out.println("count:"+object[0]);System.out.println("max:"+object[1]);
session.beginTransaction();Stringhql="from User where userName = ?";Queryquery=session.createQuery(hql);// 索引从0开始 query.setString(0,"James");List<User>userList=query.list();for(Useruser:userList){System.out.println(user.getUserName());}session.getTransaction().commit();
HQL引用占位符
12345678910111213
session.beginTransaction();Stringhql="from User where userName = :name";Queryquery=session.createQuery(hql);query.setParameter("name","James");List<User>userList=query.list();for(Useruser:userList){System.out.println(user.getUserName());}session.getTransaction().commit();
方法名称 对应SQL中的表达式
----------------------------------------------------------
Restrictions.eq field = value
Restrictions.gt field > value
Restrictions.lt field < valueRestrictions.gefield>= value
Restrictions.le field <= value
Restrictions.between field between value1 and value2
Restrictions.in field in(…)
Restrictions.and and
Restrictions.or or
Restrictions.like field like value
session.beginTransaction();Stringsql="select id,username,userpwd from t_user";Listlist=session.createSQLQuery(sql).list();for(Objectitem:list){Object[]rows=(Object[])item;System.out.println("id:"+rows[0]+"username:"+rows[1]+"userpwd:"+rows[2]);}session.getTransaction().commit();
addEntity()示例
1234567891011121314
session.beginTransaction();Stringsql="select id,username,userpwd from t_user";// addEntity()可以告诉Hibernate你想要封装成对象的类型,然后自动为你封装SQLQueryquery=session.createSQLQuery(sql).addEntity(User.class);List<User>list=query.list();for(Useruser:list){System.out.println(user.getUserName());}session.getTransaction().commit();
uniqueResult示例
12345678910
session.beginTransaction();Stringsql="select id,username,userpwd from t_user where id = 2";SQLQueryquery=session.createSQLQuery(sql).addEntity(User.class);Useruser=(User)query.uniqueResult();System.out.println(user.getUserName());session.getTransaction().commit();