码迷,mamicode.com
首页 > Web开发 > 详细

七 Hibernate5种查询检索方式,单表&多表

时间:2019-01-24 00:22:32      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:qbc   list   分组   img   demo   max   模版   多个   一个   

Hibernate中提供了5种查询方式:

  1. OID查询
  2. 对象导航查询
  3. HQL检索
  4. QBC检索
  5. SQL检索

OID检索:

Hibernate根据对象的oid(表中主键)

  • 使用get方法   Customer customer = new Customer(Customer.class , 1l);
  • 使用load方法

 

对象导航检索:

Hibernate根据一个已经查询到的对象,获得其关联对象的一种查询方式

LinkMan linkman = session.get(LinkMan.class , 1l);

linkman,getCustomer();

 

HQL检索:Hibernate Query Language

一种面向对象的查询语言,session.createQuery(hql);

  • HQL的简单查询
  • HQL的排序查询
  • HQL的条件查询
  • HQL的投影查询  : 查询对象的某个或某些属性
  • HQL的分组统计查询
  • HQL的分页查询

 

/**
 * HQL查询方式测试类
 */
public class demo1 {
    @Test
    /**
     * 初始化数据
     */
    public void demo1() {

        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        Customer customer = new Customer();
        customer.setCust_name("啥几把");
        for (int i = 0; i < 10; i++) {
            LinkMan linkMan = new LinkMan();
            linkMan.setLkm_name("啊实打" + i);
            linkMan.setCustomer(customer);
            ;
            session.save(linkMan);
        }
        session.save(customer);
        transaction.commit();
    }

    @Test
    /**
     * HQL的简单查询,sql支持*,HQL不支持*,但是聚合函数里面可以用,比如count(*)
     */
    public void demo2() {

        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();
    }

    @Test
    /**
     * HQL的别名查询
     */
    public void demo3() {

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

    @Test
    /**
     * HQL的排序查询,升序asc,降序desc
     */
    public void demo4() {

        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        // HQL的排序查询
        List<Customer> list = session.createQuery("from Customer order by cust_id desc ").list();
        for (Customer customer : list) {
            System.out.println(customer);
        }
        transaction.commit();
    }

    @Test
    /**
     * HQL的条件查询,按位置绑定,按名称绑定
     */
    public void demo5() {

        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        // 按位置绑定
        /*
         * Query query = session.
         * createQuery("from Customer where cust_name like ? and cust_id = ? ");
         * query.setParameter(0, "李兵"); query.setParameter(1, 1l);
         */
        // 按名称绑定
        Query query = session.createQuery("from Customer where cust_name like :aaa and cust_id = :bbb ");
        query.setParameter("aaa", "李%");
        query.setParameter("bbb", 1l);
        List<Customer> list = query.list();
        for (Customer customer : list) {
            System.out.println(customer);
        }
        transaction.commit();
    }

    @Test
    /**
     * HQL的投影查询,查询对象的某个或者某些属性
     */
    public void demo6() {

        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        // 投影查询,单个属性
        // List<Object> list = session.createQuery("select c.cust_name from
        // Customer c ").list();
        // 投影查询,多个属性,封装到数组
        /*
         * List<Object[]> list =
         * session.createQuery("select c.cust_name,c.cust_id from Customer c ").
         * list(); for (Object[] objects : list) {
         * System.out.println(Arrays.toString(objects)); }
         */
        // 投影查询,多个属性,封装到对象(需要提供构造)
        List<Customer> list = session.createQuery("select new Customer(cust_id,cust_name) from Customer ").list();
        for (Customer customer : list) {
            System.out.println(customer);
        }
        transaction.commit();
    }

    @Test
    /**
     * HQL的分页查询
     */
    public void demo7() {

        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        // 分页查询,单个属性
        Query query = session.createQuery("from LinkMan");
        query.setFirstResult(20);
        query.setMaxResults(10);
        List<LinkMan> list = query.list();
        for (LinkMan linkMan : list) {
            System.out.println(linkMan);
        }
        transaction.commit();
    }

    @Test
    /**
     * HQL的分组统计查询,聚合函数:count(),min(),avg(),max()
     */
    public void demo8() {

        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        // HQL的分组统计查询,得到唯一结果用uniqueResult,否则用list
        /*
         * Object object =
         * session.createQuery("select count(*) from LinkMan").uniqueResult();
         * System.out.println(object);
         */
        // 分组统计:按来源,且统计大于1的
        List<Object[]> list = session
                .createQuery("select cust_source,count(*) from Customer group by cust_source having count(*) > 1")
                .list();
        for (Object[] objects : list) {
            System.out.println(Arrays.toString(objects));
        }

        transaction.commit();

} }

 

 QBC查询:

Query By Criteria:条件查询,更加面向对象的查询方式,并不是所有方式都能用

  • 简单查询
  • 排序查询
  • 分页查询
  • 条件查询
  • 统计查询
  • 离线条件查询  DetachedCriteria(SSH整合经常用到这个模版),离线:脱离Session使用,适合条件多的查询,免于拼接JDBC语句

技术分享图片

 

 

 

/**
 *QBC查询
 */
public class HibernateDemo2 {
   @Test
   /**
    * 简单查询
    */
    public void demo1(){
        Session session = HibernateUtils.getCurrentSession();
        Transaction transaction = session.beginTransaction();
        Criteria criteria = session.createCriteria(Customer.class);
        List<Customer> list = criteria.list();
        for (Customer customer : list) {
            System.out.println(customer);
        }
        transaction.commit();
   }
    
   @Test
   /**
    * 排序查询
    */
   public void demo2(){
       Session session = HibernateUtils.getCurrentSession();
       Transaction transaction = session.beginTransaction();
       Criteria criteria = session.createCriteria(Customer.class);
       criteria.addOrder(Order.desc("cust_id"));
       List<Customer> list = criteria.list();
       for (Customer customer : list) {
        System.out.println(customer);
    }
       transaction.commit();
   }
   
   @Test
   /**
    * 分页查询
    */
   public void demo3(){
       Session session = HibernateUtils.getCurrentSession();
       Transaction transaction = session.beginTransaction();
       Criteria criteria = session.createCriteria(LinkMan.class);
       criteria.setFirstResult(10);
       criteria.setMaxResults(5);
       List<LinkMan> list = criteria.list();
       for (LinkMan linkMan : list) {
        System.out.println(linkMan);
    }
       transaction.commit();
   }
   @Test
   /**
    * 条件查询
    */
   public void demo4(){
       Session session = HibernateUtils.getCurrentSession();
       Transaction transaction = session.beginTransaction();
       Criteria criteria = session.createCriteria(Customer.class);
       /**
        * 设置条件:
        * =  eq
        * >  gt
        * >= gq
        * <  lt
        * <= le
        * <> ne
        * like
        * in
        * and 
        * or
        */
       criteria.add(Restrictions.eq("cust_source", "小广告"));
       criteria.add(Restrictions.like("cust_name", "李%"));
       List<Customer> list = criteria.list();
       for (Customer customer : list) {
        System.out.println(customer);
    }
       transaction.commit();
   }
   @Test
   /**
    * 条件查询
    */
   public void demo5(){
       Session session = HibernateUtils.getCurrentSession();
       Transaction transaction = session.beginTransaction();
       Criteria criteria = session.createCriteria(Customer.class);
       /**
        * 设置条件:
        * =  eq
        * >  gt
        * >= gq
        * <  lt
        * <= le
        * <> ne
        * like
        * in
        * and 
        * or
        */
       criteria.add(Restrictions.eq("cust_source", "小广告"));
       criteria.add(Restrictions.like("cust_name", "李%"));
       List<Customer> list = criteria.list();
       for (Customer customer : list) {
           System.out.println(customer);
       }
       transaction.commit();
   }
   
   @Test
   /**
    * 统计查询
    */
   public void demo6(){
       Session session = HibernateUtils.getCurrentSession();
       Transaction transaction = session.beginTransaction();
       Criteria criteria = session.createCriteria(Customer.class);
       /*
        * add            普通条件,where后面的
        * addOrder       :排序
        * setProjection  :聚合函数,group by having
        */
              criteria.setProjection(Projections.rowCount());
             Long num =  (Long) criteria.uniqueResult();
             System.out.println(num);
       transaction.commit();
   }
   @Test
   /**
    * 离线条件查询
    */
   public void demo7(){
       DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
       detachedCriteria.add(Restrictions.like("cust_name", "李%"));
      
       Session session = HibernateUtils.getCurrentSession();
       Transaction transaction = session.beginTransaction();
       
       Criteria criteria = detachedCriteria.getExecutableCriteria(session); 
       
       List<Customer> list = criteria.list();
       for (Customer customer : list) {
        System.out.println(customer);
    }
       transaction.commit();
   }
   
   
}

 

七 Hibernate5种查询检索方式,单表&多表

标签:qbc   list   分组   img   demo   max   模版   多个   一个   

原文地址:https://www.cnblogs.com/ltfxy/p/10312059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!