Hibernater学习笔记(六)

作者: Mr_欢先生 | 来源:发表于2018-01-02 14:17 被阅读42次
    一、对象导航查询

    之前的客户和联系人的关系是一 对多的关系:一对多

    场景:根据客户id查询客户,在查出该客户的所有联系人

     @Test
        public void selectTest(){
            SessionFactory sessionFactory = null;
            Session session = null;
            Transaction transaction = null;
            try{
                sessionFactory = HibernateUtils.getSessionFactory();
                session = sessionFactory.openSession();
                transaction = session.beginTransaction();
                //根据c_id 查询出该客户
                Customer customer = session.get(Customer.class,1);
                //然后查询出该客户联系人
                Set<Salesperson> set  = customer.getSalespersonSet();
                Iterator<Salesperson> it = set.iterator();
                while (it.hasNext()) {
                    Salesperson str = it.next();
                    System.out.println(str.getS_name());
                }
                transaction.commit();
            }catch (Exception e){
                transaction.rollback();
                e.printStackTrace();
            }finally{
                session.close();
                sessionFactory.close();
            }
        }
    
    二、HQL查询
    • 查询所有客户信息
      • 创建Query对象,写出hql语句
      • 调用query对象里面的方法得到结果
      • 查询所有 from+实体类名称
        @Test
        public void selectTest(){
            SessionFactory sessionFactory = null;
            Session session = null;
            Transaction transaction = null;
            try{
                sessionFactory = HibernateUtils.getSessionFactory();
                session = sessionFactory.openSession();
                transaction = session.beginTransaction();
                //创建query对象
                  Query query =  session.createQuery("from Customer");
    
                 //调用方法得到结果
                List<Customer> list = query.list();
                for (Customer customer: list) {
                    System.out.println(customer.getC_id()+"-"+customer.getC_name());
                }
                transaction.commit();
            }catch (Exception e){
                transaction.rollback();
                e.printStackTrace();
            }finally{
                session.close();
                sessionFactory.close();
            }
        }
    
    • 条件查询
      • 语句:from 实体类名称 where 实体类属性=? and 实体类名称
    @Test
        public void selectByCidAndCnameTest(){
            SessionFactory sessionFactory = null;
            Session session = null;
            Transaction transaction = null;
            try{
                sessionFactory = HibernateUtils.getSessionFactory();
                session = sessionFactory.openSession();
                transaction = session.beginTransaction();
                //创建query对象
                Query query =  session.createQuery("from Customer where c_id = ? and c_name = ?");
                query.setParameter(0,1);
                query.setParameter(1,"李客户");
                //调用方法得到结果
                List<Customer> list = query.list();
                for (Customer customer: list) {
                    System.out.println(customer.getC_id()+"-"+customer.getC_name());
                }
                transaction.commit();
            }catch (Exception e){
                transaction.rollback();
                e.printStackTrace();
            }finally{
                session.close();
                sessionFactory.close();
            }
        }
    
    条件查询
    • 排序查询
      • 语句:from 实体类名称 order by 实体类名称 asc/desc
    @Test
        public void selectOrderByTest(){
            SessionFactory sessionFactory = null;
            Session session = null;
            Transaction transaction = null;
            try{
                sessionFactory = HibernateUtils.getSessionFactory();
                session = sessionFactory.openSession();
                transaction = session.beginTransaction();
                //创建query对象
                Query query =  session.createQuery("from Customer order by  c_id desc ");
                //调用方法得到结果
                List<Customer> list = query.list();
                for (Customer customer: list) {
                    System.out.println(customer.getC_id()+"-"+customer.getC_name());
                }
                transaction.commit();
            }catch (Exception e){
                transaction.rollback();
                e.printStackTrace();
            }finally{
                session.close();
                sessionFactory.close();
            }
        }
    
    • 分页查询
     @Test
        public void selectLimitTest(){
            SessionFactory sessionFactory = null;
            Session session = null;
            Transaction transaction = null;
            try{
                sessionFactory = HibernateUtils.getSessionFactory();
                session = sessionFactory.openSession();
                transaction = session.beginTransaction();
                //创建query对象
                Query query =  session.createQuery("from Customer");
                //设置分页数据
                query.setFirstResult(0);
                query.setMaxResults(2);
                //调用方法得到结果
                List<Customer> list = query.list();
                for (Customer customer: list) {
                    System.out.println(customer.getC_id()+"-"+customer.getC_name());
                }
                transaction.commit();
            }catch (Exception e){
                transaction.rollback();
                e.printStackTrace();
            }finally{
                session.close();
                sessionFactory.close();
            }
        }
    
    • 投影查询
      • 语句:select * from 实体类属性名称一,实体类属性名称二, from 实体类名称
      • select 后面不能写 * 号,不支持
     @Test
        public void selectSomeTest(){
            SessionFactory sessionFactory = null;
            Session session = null;
            Transaction transaction = null;
            try{
                sessionFactory = HibernateUtils.getSessionFactory();
                session = sessionFactory.openSession();
                transaction = session.beginTransaction();
                //创建query对象
                Query query =  session.createQuery("select c_name from Customer");
                //调用方法得到结果
                List<Object> lists = query.list();
                for (Object list: lists) {
                    System.out.println(list);
                }
                transaction.commit();
            }catch (Exception e){
                transaction.rollback();
                e.printStackTrace();
            }finally{
                session.close();
                sessionFactory.close();
            }
        }
    
    image.png
    • 聚集函数使用
      • count ,sum , avg , mac , min
      • 语句:select count(*) from 实体类名称
    @Test
        public void selectHanshuTest(){
            SessionFactory sessionFactory = null;
            Session session = null;
            Transaction transaction = null;
            try{
                sessionFactory = HibernateUtils.getSessionFactory();
                session = sessionFactory.openSession();
                transaction = session.beginTransaction();
                //创建query对象
                Query query =  session.createQuery("select count(*) from Customer");
                //调用方法得到结果
                Object count = query.uniqueResult();
                //先转换成long类型,再转换为int
                Long co = Long.parseLong(count.toString());
                System.out.println(co.intValue());
                transaction.commit();
            }catch (Exception e){
                transaction.rollback();
                e.printStackTrace();
            }finally{
                session.close();
                sessionFactory.close();
            }
        }
    

    上一篇:Hibernater学习笔记(五)
    当前文集 :Hibernate框架学习
    本笔记hibernate案例:github地址

    相关文章

      网友评论

        本文标题:Hibernater学习笔记(六)

        本文链接:https://www.haomeiwen.com/subject/btjzixtx.html