hibernate---session查询
Posted more harder, the more fortunat
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate---session查询相关的知识,希望对你有一定的参考价值。
public class MyTest { public static void main(String[] args) { //查询集合 Session session = SessionUtil.openSession(); String hql = "from Emp"; Query query = session.createQuery(hql); @SuppressWarnings("unchecked") List<Emp> list = query.list(); //带条件查询方法1 String hql2 = "from Emp where empno = ?"; Query query2 = session.createQuery(hql2); query2.setInteger(0, 23); Emp emp = (Emp) query2.uniqueResult(); //带条件查询方法2 String hql3 = "from Emp where empno = ?"; Query query3 = session.createQuery(hql3); query3.setParameter(0, 23); Emp emp2 = (Emp) query2.uniqueResult(); //带条件查询方法3 String hql4 = "from Emp where empno = :xx"; Query query4 = session.createQuery(hql4); query4.setParameter("xx", 23); Emp emp3 = (Emp) query2.uniqueResult(); //分页查询 String hql5 = "from Emp"; Query query5 = session.createQuery(hql5); query5.setFirstResult(0); query5.setMaxResults(10); @SuppressWarnings("unchecked") List<Emp> list2 = query5.list(); System.out.println(list); System.out.println(emp); System.out.println(emp2); System.out.println(emp3); System.out.println(list2); } }
以上是关于hibernate---session查询的主要内容,如果未能解决你的问题,请参考以下文章
通过hibernate session.connection()获得数据库连接时,导致的查询缓慢甚至假死机问题
Hibernate session.createCriteria 与 session.get 性能
Hibernate通过配置文件构建org.hibernate.Session对象的方式与使用范例源代码