zoukankan      html  css  js  c++  java
  • hibernated的HQL查询操作总结

    只粘贴部分关键代码

    1.简单查询

              Session session = hibernateUtils.getCurrentSession();
              Transaction transaction =  session.beginTransaction();
              Query query = session.createQuery("from Customer");
              List<Customer> list = query.list();
              for(Customer customer : list) {
                 System.out.println(customer);
              }
              transaction.commit();

    2.条件查询

     Session session = hibernateUtils.getCurrentSession();
                  Transaction transaction =  session.beginTransaction();
    //            Query query = session.createQuery("from Customer where cust_name = ?");
                  Query query = session.createQuery("from Customer where cust_name = ? and cust_source like ?");
                  query.setParameter(0, "xxx");
                  query.setParameter(1, "a%");
                  List<Customer> list = query.list();
                  for(Customer customer : list) {
                     System.out.println(customer);
                  }
                  transaction.commit();

    3.投影查询

     Session session = hibernateUtils.getCurrentSession();
                  Transaction transaction =  session.beginTransaction();
    //              Query query = session.createQuery("select c.cust_name from Customer c");
    //              Query query = session.createQuery("select c.cust_name,c.cust_source from Customer c");
                  Query query = session.createQuery("select new Customer(cust_name,cust_source) from Customer c");
    //              List<Object[]> list = query.list();
                  List<Customer> list = query.list();
                  for(Customer customer : list) {
                     System.out.println(customer);
                  }
                  transaction.commit();
        

    4.分页查询

                 Session session = hibernateUtils.getCurrentSession();
                  Transaction transaction =  session.beginTransaction();          
                  Query query = session.createQuery(" from LinkMan");
                  query.setFirstResult(10);//从第几条开始查,比如这句话的意思是从第十一条开始查
                  query.setMaxResults(5);
                  List<LinkMan> list = query.list();
                  for(LinkMan man : list) {
                     System.out.println(man);
                  }
                  transaction.commit();

    5.分组统计查询

                        Session session = hibernateUtils.getCurrentSession();
                      Transaction transaction =  session.beginTransaction();          
                      List<Object[]> list = session.createQuery("select lkm_name,count(*) from LinkMan group by lkm_name").list();
                      for(Object[] objects : list) {
                          System.out.println(Arrays.toString(objects));
                      }
                      transaction.commit();
  • 相关阅读:
    ELM学习
    《进化》从孤胆极客到高效团队
    《人件》《PeopleWare》 【美】Tom DeMarco TimothyLister 著 肖然 张逸 滕云 译
    《进化》从孤胆极客到高效团队---Notes1
    大数据第一部分LInux学习Note1
    C#Windows窗体初学
    C#初学笔记(Windows编程的基本概念)
    C#学习2017-9-26(读取文本文件和读取二进制文件)Notes9
    C#学习2017-9-26Notes8(文件和流,FileStream类)
    C#学习笔记Notes8(接口,接口实现,程序集,命名空间,using)
  • 原文地址:https://www.cnblogs.com/yanqingguo/p/9757705.html
Copyright © 2011-2022 走看看