JavaEE 学习专题程序员Java学习笔记

Hibernater学习笔记(六)

2018-01-02  本文已影响42人  Mr_欢先生
一、对象导航查询

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

场景:根据客户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查询
    @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();
        }
    }
@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();
        }
    }
条件查询
@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();
        }
    }
 @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
@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地址
上一篇下一篇

猜你喜欢

热点阅读