码迷,mamicode.com
首页 > 系统相关 > 详细

Hibernate,hql语言

时间:2014-07-30 11:54:53      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   os   io   for   ar   

1、

/**
     * 查询Admin, query.list()
     */
    @Test
    public void eg1() {
        Session session = null;
        try {// 创建session
            session = HibernateUtil.getSessionFactory().openSession();
            // 声明变量,并初始化
            String hql = "from Admin";
            // 构建query对象
            Query query = session.createQuery(hql);
            // 执行查询
            List<Admin> adminList = query.list();
            // 遍历并输出结果
            for (Admin admin : adminList) {
                System.out.println("账户:" + admin.getLoginId());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }

    }

2、

/**
     * 查询Admin,query.iterator()
     */
    @Test
    public void eg2(){
        Session session = null;
        try {// 创建session
            session = HibernateUtil.getSessionFactory().openSession();
            // 声明变量,并初始化
            String hql = "from Admin";
            // 构建query对象
            Query query = session.createQuery(hql);
            // 执行查询
            Iterator<Admin> adminIterator=query.iterate();
            // 遍历并输出结果
            while (adminIterator.hasNext()) {
                Admin admin = adminIterator.next();
                System.out.println("账户:"+admin.getLoginId());                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

3、

/**
     * 设置占位符
     */
    @Test
    public void eg3(){
        Session session = null;
        try {// 创建session
            session = HibernateUtil.getSessionFactory().openSession();
            // 声明变量,并初始化
            String hql = "from Admin where loginId=?";
            // 构建query对象
            Query query = session.createQuery(hql);
            //为占位符赋值
            query.setString(0, "admin");
            // 执行查询
            Iterator<Admin> adminIterator=query.iterate();
            // 遍历并输出结果
            while (adminIterator.hasNext()) {
                Admin admin = adminIterator.next();
                System.out.println("账户密码:"+admin.getLoginPwd());                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

4、

/**
     *命名参数
     */
    @Test
    public void eg4() {
        Session session = null;
        try {// 创建session
            session = HibernateUtil.getSessionFactory().openSession();
            // 声明变量,并初始化
            String hql = "from Admin where loginId=:loginId";
            // 构建query对象
            Query query = session.createQuery(hql);
            //
            query.setString("loginId", "admin");
            // 执行查询
            Iterator<Admin> adminIterator=query.iterate();
            // 遍历并输出结果
            while (adminIterator.hasNext()) {
                Admin admin = adminIterator.next();
                System.out.println("账户密码:"+admin.getLoginPwd());                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

5、

/**
     * 设置占位符,setParameter
     */
    @Test
    public void eg5_1() {
        Session session = null;
        try {// 创建session
            session = HibernateUtil.getSessionFactory().openSession();
            // 声明变量,并初始化
            String hql = "from Admin where loginId=?";
            // 构建query对象
            Query query = session.createQuery(hql);
            //为占位符赋值
            query.setParameter(0, "admin");
            // 执行查询
            Iterator<Admin> adminIterator=query.iterate();
            // 遍历并输出结果
            while (adminIterator.hasNext()) {
                Admin admin = adminIterator.next();
                System.out.println("账户密码:"+admin.getLoginPwd());                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

6、

/**
     * setParameter
     */
    @Test
    public void eg5_2(){
        Session session = null;
        try {// 创建session
            session = HibernateUtil.getSessionFactory().openSession();
            // 声明变量,并初始化
            String hql = "from Admin where loginId=:loginId";
            // 构建query对象
            Query query = session.createQuery(hql);
            //
            query.setParameter("loginId", "admin");
            // 执行查询
            Iterator<Admin> adminIterator=query.iterate();
            // 遍历并输出结果
            while (adminIterator.hasNext()) {
                Admin admin = adminIterator.next();
                System.out.println("账户密码:"+admin.getLoginPwd());                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

7、

/**
     * 动态设置查询参数
     */
    @Test
    public void eg6() throws Exception{
        Session session = null;
        try {// 创建session
            session = HibernateUtil.getSessionFactory().openSession();
            //准备查询条件
            Admin admin=new Admin();
            admin.setLoginId("admin");
            //准备hql,hql根据条件动态生成

            StringBuilder hql=new StringBuilder("from Admin as emp where 1=1");
            if(null!=admin.getClass()){
                hql.append(" and loginId=:loginId");
            }
            // 构建query对象
            Query query = session.createQuery(hql.toString());
            //使用query对象的setProperties()方法为参数赋值
            query.setProperties(admin);
            // 执行查询
            Iterator<Admin> adminIterator=query.iterate();
            // 遍历并输出结果
            while (adminIterator.hasNext()) {
                System.out.println("账户密码:"+adminIterator.next().getLoginPwd());                
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

8、

/**
     * uniqueResult
     */
    public void eg7(){
        Session session = null;
        try {// 创建session
            session = HibernateUtil.getSessionFactory().openSession();
            // 声明变量,并初始化
            String hql = "from Admin where loginId=?";
            // 构建query对象
            Query query = session.createQuery(hql);
            
            query.setParameter(0, "%ad");
            // 执行查询
            Admin admin=(Admin) query.uniqueResult();
            System.out.println("账户密码:"+admin.getLoginPwd());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

9、

 

Hibernate,hql语言,布布扣,bubuko.com

Hibernate,hql语言

标签:style   blog   color   使用   os   io   for   ar   

原文地址:http://www.cnblogs.com/shenlan/p/3877258.html

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