加载中...

Hibernate(十)HQL查询二


一、数据库的emp名和dept表

建立持久化类和配置文件,可以用MyEclipse直接生成

持久化类

  1. package entity;
  2. import java.util.Date;
  3. public class Emp implements java.io.Serializable {
  4. // Fields
  5.  
  6. private Short empno;
  7. private Dept dept;
  8. private String ename;
  9. private String job;
  10. private Short mgr;
  11. private Date hiredate;
  12. private Double sal;
  13. private Double comm;
  14. // Constructors
  15.  
  16. /** default constructor */
  17. public Emp() {
  18. }
  19. /** minimal constructor */
  20. public Emp(Short empno) {
  21. this.empno = empno;
  22. }
  23. /** full constructor */
  24. public Emp(Short empno, Dept dept, String ename, String job, Short mgr,
  25. Date hiredate, Double sal, Double comm) {
  26. this.empno = empno;
  27. this.dept = dept;
  28. this.ename = ename;
  29. this.job = job;
  30. this.mgr = mgr;
  31. this.hiredate = hiredate;
  32. this.sal = sal;
  33. this.comm = comm;
  34. }
  35. // Property accessors
  36.  
  37. public Short getEmpno() {
  38. return this.empno;
  39. }
  40. public void setEmpno(Short empno) {
  41. this.empno = empno;
  42. }
  43. public Dept getDept() {
  44. return this.dept;
  45. }
  46. public void setDept(Dept dept) {
  47. this.dept = dept;
  48. }
  49. public String getEname() {
  50. return this.ename;
  51. }
  52. public void setEname(String ename) {
  53. this.ename = ename;
  54. }
  55. public String getJob() {
  56. return this.job;
  57. }
  58. public void setJob(String job) {
  59. this.job = job;
  60. }
  61. public Short getMgr() {
  62. return this.mgr;
  63. }
  64. public void setMgr(Short mgr) {
  65. this.mgr = mgr;
  66. }
  67. public Date getHiredate() {
  68. return this.hiredate;
  69. }
  70. public void setHiredate(Date hiredate) {
  71. this.hiredate = hiredate;
  72. }
  73. public Double getSal() {
  74. return this.sal;
  75. }
  76. public void setSal(Double sal) {
  77. this.sal = sal;
  78. }
  79. public Double getComm() {
  80. return this.comm;
  81. }
  82. public void setComm(Double comm) {
  83. this.comm = comm;
  84. }
  85. }
Emp持久化类
  1. package entity;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. public class Dept implements java.io.Serializable {
  5. // Fields
  6.  
  7. private Byte deptno;
  8. private String dname;
  9. private String loc;
  10. private Set emps = new HashSet(0);
  11. // Constructors
  12.  
  13. /** default constructor */
  14. public Dept() {
  15. }
  16. /** minimal constructor */
  17. public Dept(Byte deptno) {
  18. this.deptno = deptno;
  19. }
  20. /** full constructor */
  21. public Dept(Byte deptno, String dname, String loc, Set emps) {
  22. this.deptno = deptno;
  23. this.dname = dname;
  24. this.loc = loc;
  25. this.emps = emps;
  26. }
  27. // Property accessors
  28.  
  29. public Byte getDeptno() {
  30. return this.deptno;
  31. }
  32. public void setDeptno(Byte deptno) {
  33. this.deptno = deptno;
  34. }
  35. public String getDname() {
  36. return this.dname;
  37. }
  38. public void setDname(String dname) {
  39. this.dname = dname;
  40. }
  41. public String getLoc() {
  42. return this.loc;
  43. }
  44. public void setLoc(String loc) {
  45. this.loc = loc;
  46. }
  47. public Set getEmps() {
  48. return this.emps;
  49. }
  50. public void setEmps(Set emps) {
  51. this.emps = emps;
  52. }
  53. }
Dept持久化类

emp类配置文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4.  
  5. <hibernate-mapping>
  6. <class name="entity.Emp" table="EMP" schema="SCOTT">
  7. <id name="empno" type="java.lang.Short">
  8. <column name="EMPNO" precision="4" scale="0" />
  9. <generator class="assigned" />
  10. </id>
  11. <many-to-one name="dept" class="entity.Dept" fetch="select">
  12. <column name="DEPTNO" precision="2" scale="0" />
  13. </many-to-one>
  14. <property name="ename" type="java.lang.String">
  15. <column name="ENAME" length="10" />
  16. </property>
  17. <property name="job" type="java.lang.String">
  18. <column name="JOB" length="9" />
  19. </property>
  20. <property name="mgr" type="java.lang.Short">
  21. <column name="MGR" precision="4" scale="0" />
  22. </property>
  23. <property name="hiredate" type="java.util.Date">
  24. <column name="HIREDATE" length="7" />
  25. </property>
  26. <property name="sal" type="java.lang.Double">
  27. <column name="SAL" precision="7" />
  28. </property>
  29. <property name="comm" type="java.lang.Double">
  30. <column name="COMM" precision="7" />
  31. </property>
  32. </class>
  33. </hibernate-mapping>
Emp.hbm.xml

 

dept类配置文件 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  4.  
  5. <hibernate-mapping>
  6. <class name="entity.Dept" table="DEPT" schema="SCOTT">
  7. <id name="deptno" type="java.lang.Byte">
  8. <column name="DEPTNO" precision="2" scale="0" />
  9. <generator class="assigned" />
  10. </id>
  11. <property name="dname" type="java.lang.String">
  12. <column name="DNAME" length="14" />
  13. </property>
  14. <property name="loc" type="java.lang.String">
  15. <column name="LOC" length="13" />
  16. </property>
  17. <set name="emps" inverse="true">
  18. <key>
  19. <column name="DEPTNO" precision="2" scale="0" />
  20. </key>
  21. <one-to-many class="entity.Emp" />
  22. </set>
  23. </class>
  24. </hibernate-mapping>
Dept.hbm.xml

 

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7.  
  8. <session-factory>
  9. <property name="dialect">
  10. org.hibernate.dialect.Oracle9Dialect
  11. </property>
  12. <property name="connection.url">
  13. jdbc:oracle:thin:@localhost:1521:orcl
  14. </property>
  15. <property name="connection.username">root</property>
  16. <property name="connection.password">root</property>
  17. <property name="connection.driver_class">
  18. oracle.jdbc.OracleDriver
  19. </property>
  20. <mapping resource="entity/Emp.hbm.xml" />
  21. <mapping resource="entity/Dept.hbm.xml" />
  22.  
  23. </session-factory>
  24.  
  25. </hibernate-configuration>
hibernate.cfg.xml

 

二、Query接口分页实现

  • 根据结果获得总亡录数:
  1. Querty query=session.createQuery("from Emp");
  2. List list=query.list();
  3. int count=list.size();
  • 计算总页数:
  1. int totalPages=(count%pageSize==0)?(count/pageSize):(count/pageSize+1);
  • 实现分页:
  1. query.setFirstResult((pageIndex-1)*pageSize); //开始记录数
  2. query.setMaxResults(pageSize);//每页显示最大记录数
  3. List resultList=query.list(); //每页显示的记录集合

 查询全部员工

  1. /*
  2. * 查询员工表内容
  3. */
  4. public static void findEmp(){
  5. Session session=new Configuration().configure().buildSessionFactory().openSession();
  6. //查询员工表按入职日期升序排,日期相同按名字降序
  7. String hql=" from Emp e order by e.hiredate ,e.ename desc ";
  8. Query query=session.createQuery(hql);
  9. List<Emp> emps=query.list();
  10. for (Emp emp : emps) {
  11. System.out.println("员工编号:"+emp.getEmpno()+"\t姓名: "+emp.getEname()+"\t入职日期:"+emp.getHiredate()+"\t部门名称:"+emp.getDept().getDname());
  12. }
  13. }

分页查询

  1. /*
  2. * 分页查询
  3. */
  4. public static void pageDisplay(){
  5. Session session=new Configuration().configure().buildSessionFactory().openSession();
  6. String hql="from Emp";
  7. //第几页
  8. int pageIndex=4;
  9. //每页显示记录数
  10. int pageSize=4;
  11. Query query=session.createQuery(hql);
  12. //起始记录数
  13. query.setFirstResult((pageIndex-1)*pageSize);
  14. //每页显示最多记录数
  15. query.setMaxResults(pageSize);
  16. List<Emp> emps=query.list();
  17. for (Emp emp : emps) {
  18. System.out.println("员工编号:"+emp.getEmpno()+"\t姓名: "+emp.getEname()+"\t入职日期:"+emp.getHiredate()+"\t部门名称:"+emp.getDept().getDname());
  19. }
  20. }

三、连接查询

部门表中有个40部门,员工表中没有人是40部门的,

首先向EMP表中插入一条没有部门的记录

  1. insert into emp(empno,ename,job,hiredate,sal)values(8888,'张三','业务员',sysdate,800);

3.1、查询部门的所有员工使用内连接

  1. String hql="from Dept d inner join d.emps";

3.2、查询部门的所有员工使用左外连接

  1. String hql="from Dept d left join d.emps";

3.3、查询部门的所有员工使用右外连接

  1. String hql="from Dept d right join d.emps";

 


还没有评论.