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

Hibernate框架之路(四)hibernate查询方式

时间:2017-08-31 10:56:17      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:test   tran   实体类   cti   cep   模糊查询   实体   list   on()   

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查询方式

标签:test   tran   实体类   cti   cep   模糊查询   实体   list   on()   

原文地址:http://www.cnblogs.com/yaowan/p/7456724.html

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