Hibernate框架之路hibernate查询方式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate框架之路hibernate查询方式相关的知识,希望对你有一定的参考价值。

OID查询

    /**
     * OID 查询
     * 根据ID查询
     * 一对多查询
     */
    @Test
    public void test() {
        SessionFactory factory = null;
        Session session = null;
        Transaction ts = null;
        try {
            factory = HibernateUtils.getSessionFactory();
            session = factory.openSession();
            ts = session.beginTransaction();
//            查询客户所拥有的联系人;先查询客户 ID=1的客户;根据客户查询联系人
            User user = session.get(User.class, 1);
            //获得联系人的set集合
            Set<Link> link = user.getLink();
//            输出联系人个数
            System.out.println(link.size());
            ts.commit();
        } catch (Exception e) {
            ts.rollback();
        }finally{
            session.close();
            factory.close();
        }
    }

HQL查询所有

    /**
     *查询所有
     *hql
     */
    @Test
    public void test2() {
        SessionFactory factory = null;
        Session session = null;
        Transaction ts = null;
        try {
            factory = HibernateUtils.getSessionFactory();
            session = factory.openSession();
            ts = session.beginTransaction();
//            查询所有客户    from 客户实体类名称
            Query query = session.createQuery("from User");
            List<User> list = query.list();
            for (User user : list) {
                System.out.println(user.getuId()+"\t"+user.getuName());
            }
            System.out.println("-------------------------");
            Query query2 = session.createQuery("from Link");
            List<Link> list2 = query2.list();
            for (Link link : list2) {
                System.out.println(link.getcId()+"\t"+link.getcName());
            }
            ts.commit();
        } catch (Exception e) {
            ts.rollback();
        }finally{
            session.close();
            factory.close();
        }
    }

Hql条件查询

@Test
    public void test2() {
        SessionFactory factory = null;
        Session session = null;
        Transaction ts = null;
        try {
            factory = HibernateUtils.getSessionFactory();
            session = factory.openSession();
            ts = session.beginTransaction();
            //创建query对象    from Cuastomer where user_id=? and user_name=?
            Query query = session.createQuery("from User c where c.uId=? and c.uName=?");
            /**
             * 向占位符里填写数据;占位符下标从0开始
             * 第一个参数为下标
             * 第二个参数为值
             */
            query.setParameter(0, 1);
            query.setParameter(1, "张三");
            List<User> list = query.list();
            for (User user : list) {
                System.out.println(user.getuTel());
            }
            ts.commit();
        } catch (Exception e) {
            ts.rollback();
        }finally{
            session.close();
            factory.close();
        }
    }

hql模糊查询

    @Test
    public void test2() {
        SessionFactory factory = null;
        Session session = null;
        Transaction ts = null;
        try {
            factory = HibernateUtils.getSessionFactory();
            session = factory.openSession();
            ts = session.beginTransaction();
            //创建query对象    
            Query query = session.createQuery("from User c where c.uName like ?");
            /**
             * 向占位符里填写数据;占位符下标从0开始
             * 第一个参数为下标
             * 第二个参数为值
             */
            query.setParameter(0, "%张%");
            List<User> list = query.list();
            for (User user : list) {
                System.out.println(user.getuTel());
            }
            ts.commit();
        } catch (Exception e) {
            ts.rollback();
        }finally{
            session.close();
            factory.close();
        }
    }

hql排序

    @Test
    public void test2() {
        SessionFactory factory = null;
        Session session = null;
        Transaction ts = null;
        try {
            factory = HibernateUtils.getSessionFactory();
            session = factory.openSession();
            ts = session.beginTransaction();
            //创建query对象    desc 倒序     asc正序
            Query query = session.createQuery("from User order by uId asc");
        
            List<User> list = query.list();
            for (User user : list) {
                System.out.println(user.getuId());
            }
            ts.commit();
        } catch (Exception e) {
            ts.rollback();
        }finally{
            session.close();
            factory.close();
        }
    }

HQL分页

    @Test
    public void test2() {
        SessionFactory factory = null;
        Session session = null;
        Transaction ts = null;
        try {
            factory = HibernateUtils.getSessionFactory();
            session = factory.openSession();
            ts = session.beginTransaction();
            //创建query对象    
            Query query = session.createQuery("from User");
            query.setFetchSize(0);//开始位置
            query.setMaxResults(3);    //每页显示个数    
            List<User> list = query.list();
            for (User user : list) {
                System.out.println(user.getuId());
            }
            ts.commit();
        } catch (Exception e) {
            ts.rollback();
        }finally{
            session.close();
            factory.close();
        }
    }

hql查询某个字段

    @Test
    public void test2() {
        SessionFactory factory = null;
        Session session = null;
        Transaction ts = null;
        try {
            factory = HibernateUtils.getSessionFactory();
            session = factory.openSession();
            ts = session.beginTransaction();
            //创建query对象    
            Query query = session.createQuery("select uId from User");
            List<Object> list = query.list();
            for (Object object : list) {
                System.out.println(object);
            }
            ts.commit();
        } catch (Exception e) {
            ts.rollback();
        }finally{
            session.close();
            factory.close();
        }
    }

hql函数查询

    @Test
    public void test2() {
        SessionFactory factory = null;
        Session session = null;
        Transaction ts = null;
        try {
            factory = HibernateUtils.getSessionFactory();
            session = factory.openSession();
            ts = session.beginTransaction();
            //创建query对象    
            Query query = session.createQuery("select count(*) from User");
            Object obj = query.uniqueResult();
            Long lbj = (long) obj;
            int count =    lbj.intValue();
            System.out.println(count);    
            ts.commit();
        } catch (Exception e) {
            ts.rollback();
        }finally{
            session.close();
            factory.close();
        }
    }

 

以上是关于Hibernate框架之路hibernate查询方式的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate框架之路多对多多关系

Hibernate学习之路

从JDBC到hibernate再到mybatis之路

[原创]java WEB学习笔记90:Hibernate学习之路-- -HQL检索方式,分页查询,命名查询语句,投影查询,报表查询

Hibernate相关的查询 --Hibernate框架基础

java框架hibernate多对多如何进行关联查询