Hibernate,hql语言

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语言,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。