• hql语法002


    1、

    package cn.jbit.hibernatedemo.test;
    
    import java.util.Iterator;
    import java.util.List;
    
    import org.hibernate.*;
    import org.junit.Test;
    
    import cn.jbit.hibernatedemo.dao.HibernateUtil;
    import cn.jbit.hibernatedemo.entity.DeptSalary;
    
    public class Eg {
    
        /**
         * 统计部门个数。
         */
        @Test
        public void egDept() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Long count = (Long) session.createQuery(
                        "select count(*) from Dept d").uniqueResult();
                System.out.println(count);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 统计员工工资。
         */
        @Test
        public void egEmp() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Double salarySum = (Double) session.createQuery(
                        "select sum(e.salary) from Emp e").uniqueResult();
                System.out.println(salarySum);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 统计员工最低工资。
         */
        @Test
        public void egEmpMinSalary() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Double salary = (Double) session.createQuery(
                        "select min(e.salary) from Emp e").uniqueResult();
                System.out.println(salary);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 统计员工最高工资。
         */
        @Test
        public void egEmpMaxSalary() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Double salary = (Double) session.createQuery(
                        "select max(e.salary) from Emp e").uniqueResult();
                System.out.println(salary);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 统计员工平均工资。
         */
        @Test
        public void egEmpAvgSalary() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Double salary = (Double) session.createQuery(
                        "select avg(e.salary) from Emp e").uniqueResult();
                System.out.println(salary);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 统计最低工资、最高工资以及平均工资。
         */
        @Test
        public void egEmpSalary() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Object[] salarys = (Object[]) session.createQuery(
                        "select min(salary),max(salary),avg(salary) from Emp")
                        .uniqueResult();
                System.out
                        .println(salarys[0] + "," + salarys[1] + "," + salarys[2]);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 统计员工职位个数。
         */
        @Test
        public void egJobEmp() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Object count = session.createQuery(
                        "select count(distinct job) from Emp ").uniqueResult();
                System.out.println(count.getClass().getName());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 按职位统计员工个数。
         */
        @Test
        public void eg1() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                List<Object[]> list = session.createQuery(
                        "select job,count(e) from Emp e group by job").list();
                for (Object[] obj : list)
                    System.out.println(obj[0] + "," + obj[1]);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 统计各个部门的平均工资
         */
        @Test
        public void eg2() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Iterator<Object[]> it = session
                        .createQuery(
                                "select e.dept.deptName,avg(e.salary) from Emp e group by e.dept.deptName")
                        .list().iterator();
                Object[] obj = null;
                while (it.hasNext()) {
                    obj = it.next();
                    System.out.println(obj[0] + "," + obj[1]);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 统计各个职位的最低工资和最高工资
         */
        @Test
        public void eg3() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Iterator<Object[]> it = session
                        .createQuery(
                                "select job,min(salary),max(salary) from Emp group by job")
                        .list().iterator();
                Object[] obj = null;
                while (it.hasNext()) {
                    obj = it.next();
                    System.out.println(obj[0] + "," + obj[1] + "," + obj[2]);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 统计各个部门平均工资高于4000元的部门名称,打印部门名称、部门平均工资
         */
        @Test
        public void eg4() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Iterator<Object[]> it = session
                        .createQuery(
                                "select e.dept.deptName,avg(e.salary) from Emp "
                                        + "e group by e.dept.deptName having avg(e.salary)>4000")
                        .list().iterator();
                Object[] obj = null;
                while (it.hasNext()) {
                    obj = it.next();
                    System.out.println(obj[0] + "," + obj[1]);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
        /**
         * 统计各个部门平均工资高于4000元的部门名称,打印部门名称、部门平均工资,使用JavaBean封装查询结果
         */
        @Test
        public void eg4JavaBean() {
            Session session = null;
            try {
                // 获取session
                session = HibernateUtil.currentSession();
                Iterator<DeptSalary> it = session
                        .createQuery(
                                "select new cn.jbit.hibernatedemo.entity.DeptSalary(e.dept.deptName,avg(e.salary))"
                                        + " from Emp e group by e.dept.deptName having avg(e.salary)>4000")
                        .list().iterator();
                DeptSalary deptSalary = null;
                while (it.hasNext()) {
                    deptSalary = it.next();
                    System.out.println(deptSalary.getDeptName() + ","
                            + deptSalary.getAvgSalary());
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭session
                HibernateUtil.closeSession();
            }
        }
    
    
    }
  • 相关阅读:
    LightningChartJS2.0即将火热推出,敬请期待!
    html转word
    Windows上使用Python2.7安装pip
    人工智能?.netcore一样胜任!
    远程浏览服务器上的文件
    C# winform间窗体传值简单Demo
    C#发送QQ邮箱
    各种文件用JS转Base64之后的data类型
    当你的VS2019没法自动补全了,或者自动补全按回车直接换行的时候
    easyUI filebox 获取文件对象
  • 原文地址:https://www.cnblogs.com/syjp/p/11078764.html
Copyright © 2020-2023  润新知