hql syntax 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 (); the System.out .println (salarys [0] + "," + salarys [. 1] + "," + salarys [2 ]);} the catch (Exception E) {e.printStackTrace ();} the finally {// Close the session HibernateUtil.closeSession ();}} / ** * count the number of staff positions. * / @Test public void egJobEmp () {the Session the session = null ; the try {// Get the session = the session HibernateUtil.currentSession (); Object COUNT = session.createQuery ( "SELECT COUNT (DISTINCT Job) from Emp" ) .uniqueResult ( ); System.out.println (count.getClass () getName ());.} the catch (Exception E) {e.printStackTrace ();} the finally {// Close the session HibernateUtil.closeSession ();}} / ** * according to statistics the number of staff positions. * / @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] + ","+ R 2]);}} The catch (Exception E) {e.printStackTrace ();} the finally {// Close the session HibernateUtil.closeSession ();}} / ** * Statistics departments than the average salary department name $ 4,000, printing department name, department average wage * / @Test public void EG4 () {the Session the session = null ; the try {// Get the session = the session HibernateUtil.currentSession (); the Iterator <Object []> IT = the 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 [] = null obj ; the while (it.hasNext ()) {obj = it.next (); System.out.println (obj [0] + "," + obj [. 1 ]);}} the catch (Exception E) {e.printStackTrace ();} the finally {// Close sessionHibernateUtil.closeSession (); department name}} / ** * Statistics departments than the average salary $ 4,000, the printing department name, the average wage sector, packaging using JavaBean query results * / @Test public void eg4JavaBean () {the session the Session null = ; the try {// Get the session = the session HibernateUtil.currentSession (); the Iterator <DeptSalary> = the session .createQuery ( "SELECT new new cn.jbit.hibernatedemo.entity.DeptSalary (e.dept.deptName, AVG (E. the salary)) "+" E Group from Emp by e.dept.deptName HAVING AVG (e.salary)> 4000 " ) .list () Iterator ();. DeptSalary deptSalary = null ; the while (it.hasNext ()) { = deptSalary it.next (); System.out.println (deptSalary.getDeptName () + "," + deptSalary.getAvgSalary ());}} the catch (Exception E) {e.printStackTrace ();The finally} {// Close session HibernateUtil.closeSession(); } } }

 

Guess you like

Origin www.cnblogs.com/syjp/p/11078764.html