HQL基础Query简单查询结果for输出和Iterator输出

时间:2022-05-22 08:34:14

HQL第一次课:

  hibernate Query Language:hibernate 查询语言

  

  语法:

  query:

    String hql="from dept";

    Query query=session.createQuery(hql);//返回list集合

    query是一个对象,拥有很多方法,.list()方法返回集合;

  hql语句下次笔记详解,本次制作简单操作!

    

     public static List<Dept> getDept(){
List<Dept> list=new ArrayList<Dept>();
Configuration conf=null;
SessionFactory factory=null;
Session session=null; try { conf=new Configuration().configure();
//创建sessionfactory
factory=conf.buildSessionFactory();
//打开会话
session=factory.openSession();
//hql
String hql="select dept from Dept as dept where dept.deptname like '%发%'";
Query query=session.createQuery(hql);
// list=query.list();
// for(Dept dep:list){
// System.out.println(dep);
//
// }
System.out.println("迭代器");
Iterator<Dept> ite=query.iterate();
while(ite.hasNext()){
System.out.println(ite.next()); } } catch (Exception e) {
e.printStackTrace(); }finally{
if(session!=null){
session.close(); }
} return list; }