1.创建如下javaweb项目结构
2.在项目的src下创建hibernate.cfg.xml主配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" > 3 <hibernate-configuration> 4 <session-factory> 5 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 6 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> 7 <property name="connection.username">scott</property> 8 <property name="connection.password">tiger</property> 9 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> 10 <property name="show_sql">true</property> 11 <property name="format_sql">true</property> 12 <mapping resource="com/entity/Dept.hbm.xml"/> 13 <mapping resource="com/entity/Emp.hbm.xml"/> 14 </session-factory> 15 </hibernate-configuration>
3.在项目的src下的com.util包下创建HibernateUtil.java
1 package com.util; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.SessionFactory; 6 import org.hibernate.cfg.Configuration; 7 8 public class HibernateUtil { 9 private static ThreadLocal<Session> thread=new ThreadLocal<Session>(); 10 private static Configuration config=null; 11 private static SessionFactory factory=null; 12 /** 13 * 读取配置文件 14 */ 15 static{ 16 try { 17 config=new Configuration().configure("/hibernate.cfg.xml"); 18 factory=config.buildSessionFactory(); 19 } catch (HibernateException e) { 20 System.out.println("读取配置文件失败或创建factory失败"); 21 e.printStackTrace(); 22 } 23 } 24 /** 25 * 打开session 26 * @return 27 */ 28 public static Session getSession(){ 29 Session session =thread.get(); 30 if(session==null){ 31 session=factory.openSession(); 32 thread.set(session); 33 } 34 return session; 35 } 36 /** 37 * 关闭session 38 */ 39 public static void closeSession(){ 40 Session session =thread.get(); 41 thread.set(null); 42 session.close(); 43 44 } 45 46 }
4.在项目的src下的com.entity包下创建Dept.java
1 package com.entity; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 public class Dept { 7 private Integer deptno; 8 private String dname; 9 private String loc; 10 private Set<Emp> emps =new HashSet<Emp>(); 11 12 public Dept() { 13 } 14 15 public Dept(Integer deptno, String dname, String loc) { 16 this.deptno = deptno; 17 this.dname = dname; 18 this.loc = loc; 19 } 20 21 public Dept(Integer deptno, String dname, String loc, Set<Emp> emps) { 22 this.deptno = deptno; 23 this.dname = dname; 24 this.loc = loc; 25 this.emps = emps; 26 } 27 public Integer getDeptno() { 28 return deptno; 29 } 30 public void setDeptno(Integer deptno) { 31 this.deptno = deptno; 32 } 33 public String getDname() { 34 return dname; 35 } 36 public void setDname(String dname) { 37 this.dname = dname; 38 } 39 public String getLoc() { 40 return loc; 41 } 42 public void setLoc(String loc) { 43 this.loc = loc; 44 } 45 public Set<Emp> getEmps() { 46 return emps; 47 } 48 public void setEmps(Set<Emp> emps) { 49 this.emps = emps; 50 } 51 52 @Override 53 public String toString() { 54 return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]"; 55 } 56 57 58 59 }
5.在项目的src下的com.entity包下创建Emp.java
1 package com.entity; 2 3 4 5 public class Emp { 6 private Integer empno; 7 private String ename; 8 private String job; 9 private Integer sal; 10 private Dept dept; 11 12 public Emp() { 13 } 14 15 public Emp(Integer empno, String ename, String job, Integer sal) { 16 super(); 17 this.empno = empno; 18 this.ename = ename; 19 this.job = job; 20 this.sal = sal; 21 } 22 23 public Emp(Integer empno, String ename, String job, Integer sal, Dept dept) { 24 super(); 25 this.empno = empno; 26 this.ename = ename; 27 this.job = job; 28 this.sal = sal; 29 this.dept = dept; 30 } 31 32 public Integer getEmpno() { 33 return empno; 34 } 35 36 public void setEmpno(Integer empno) { 37 this.empno = empno; 38 } 39 40 public String getEname() { 41 return ename; 42 } 43 44 public void setEname(String ename) { 45 this.ename = ename; 46 } 47 48 public String getJob() { 49 return job; 50 } 51 52 public void setJob(String job) { 53 this.job = job; 54 } 55 56 public Integer getSal() { 57 return sal; 58 } 59 60 public void setSal(Integer sal) { 61 this.sal = sal; 62 } 63 64 public Dept getDept() { 65 return dept; 66 } 67 68 public void setDept(Dept dept) { 69 this.dept = dept; 70 } 71 72 @Override 73 public String toString() { 74 return "Emp [ empno=" + empno + ", ename=" + ename 75 + ", job=" + job + ", sal=" + sal + "]"; 76 } 77 78 79 80 }
6.在项目的src下的com.entity包下创建Dept.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Dept" table="DEPT"> 5 <id name="deptno" type="java.lang.Integer" column="DEPTNO"> 6 <generator class="assigned"/> 7 </id> 8 <property name="dname" type="java.lang.String" column="DNAME"/> 9 <property name="loc" type="java.lang.String" column="LOC"/> 10 <!-- 一对多 --> 11 <set name="emps" inverse="true" cascade="save-update"> 12 <key column="DEPTNO"></key> 13 <one-to-many class="com.entity.Emp"/> 14 </set> 15 </class> 16 17 </hibernate-mapping>
7.在项目的src下的com.entity包下创建Emp.hbm.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > 3 <hibernate-mapping> 4 <class name="com.entity.Emp" table="EMP"> 5 <id name="empno" type="java.lang.Integer" column="EMPNO"> 6 <generator class="assigned"/> 7 </id> 8 <property name="ename" type="java.lang.String" column="ENAME"/> 9 <property name="job" type="java.lang.String" column="JOB"/> 10 <property name="sal" type="java.lang.Integer" column="SAL"/> 11 <!-- 多对一 --> 12 <many-to-one name="dept" class="com.entity.Dept" column="DEPTNO"/> 13 </class> 14 15 <!-- 命名查询节点 --> 16 <query name="findEmpByJob"> 17 <!-- cdata节点放入hql语句,原生sql查询语句 --> 18 <![CDATA[ 19 from Emp e where e.job=:pjob 20 ]]> 21 </query> 22 </hibernate-mapping>
8.在项目的src下的com.dao包下创建DeptDao.java
1 package com.dao; 2 3 import java.util.List; 4 5 import org.hibernate.Query; 6 import org.hibernate.Session; 7 8 import com.entity.Emp; 9 import com.util.HibernateUtil; 10 11 public class EmpDao { 12 13 14 public static void main(String[] args) { 15 System.out.println("-----------1.关联查询-----------"); 16 getEmp(); 17 18 19 } 20 21 /** 22 *1.命名查询 23 */ 24 private static void getEmp() { 25 Session session=HibernateUtil.getSession(); 26 /*根据映射文件的query节点的name属性值获取hql语句*/ 27 Query query=session.getNamedQuery("findEmpByJob"); 28 /*给命名参数赋值*/ 29 //query.setString("pjob", "CLERK"); 30 query.setParameter("pjob", "CLERK"); 31 List<Emp> list=query.list(); 32 for (Emp emp : list) { 33 System.out.println(emp.getDept().getDeptno()); 34 System.out.println(emp); 35 } 36 HibernateUtil.closeSession(); 37 38 } 39 40 }
9.运行结果如下:remdeme.txt
1 1.内连接 2 Hibernate: 3 select 4 dept0_.DEPTNO as DEPTNO0_0_, 5 emps1_.EMPNO as EMPNO1_1_, 6 dept0_.DNAME as DNAME0_0_, 7 dept0_.LOC as LOC0_0_, 8 emps1_.ENAME as ENAME1_1_, 9 emps1_.JOB as JOB1_1_, 10 emps1_.SAL as SAL1_1_, 11 emps1_.DEPTNO as DEPTNO1_1_ 12 from 13 DEPT dept0_ 14 inner join 15 EMP emps1_ 16 on dept0_.DEPTNO=emps1_.DEPTNO 17 2.迫切内连接 18 select 19 dept0_.DEPTNO as DEPTNO0_0_, 20 emps1_.EMPNO as EMPNO1_1_, 21 dept0_.DNAME as DNAME0_0_, 22 dept0_.LOC as LOC0_0_, 23 emps1_.ENAME as ENAME1_1_, 24 emps1_.JOB as JOB1_1_, 25 emps1_.SAL as SAL1_1_, 26 emps1_.DEPTNO as DEPTNO1_1_, 27 emps1_.DEPTNO as DEPTNO0__, 28 emps1_.EMPNO as EMPNO0__ 29 from 30 DEPT dept0_ 31 inner join 32 EMP emps1_ 33 on dept0_.DEPTNO=emps1_.DEPTNO 34 -------4.左连接------- 35 Hibernate: 36 select 37 dept0_.DEPTNO as DEPTNO0_0_, 38 emps1_.EMPNO as EMPNO1_1_, 39 dept0_.DNAME as DNAME0_0_, 40 dept0_.LOC as LOC0_0_, 41 emps1_.ENAME as ENAME1_1_, 42 emps1_.JOB as JOB1_1_, 43 emps1_.SAL as SAL1_1_, 44 emps1_.DEPTNO as DEPTNO1_1_ 45 from 46 DEPT dept0_ 47 left outer join 48 EMP emps1_ 49 on dept0_.DEPTNO=emps1_.DEPTNO 50 51 -------5.迫切左连接------- 52 Hibernate: 53 select 54 distinct dept0_.DEPTNO as DEPTNO0_0_, 55 emps1_.EMPNO as EMPNO1_1_, 56 dept0_.DNAME as DNAME0_0_, 57 dept0_.LOC as LOC0_0_, 58 emps1_.ENAME as ENAME1_1_, 59 emps1_.JOB as JOB1_1_, 60 emps1_.SAL as SAL1_1_, 61 emps1_.DEPTNO as DEPTNO1_1_, 62 emps1_.DEPTNO as DEPTNO0__, 63 emps1_.EMPNO as EMPNO0__ 64 from 65 DEPT dept0_ 66 left outer join 67 EMP emps1_ 68 on dept0_.DEPTNO=emps1_.DEPTNO 69 -------6.右连接------- 70 Hibernate: 71 select 72 dept0_.DEPTNO as DEPTNO0_0_, 73 emps1_.EMPNO as EMPNO1_1_, 74 dept0_.DNAME as DNAME0_0_, 75 dept0_.LOC as LOC0_0_, 76 emps1_.ENAME as ENAME1_1_, 77 emps1_.JOB as JOB1_1_, 78 emps1_.SAL as SAL1_1_, 79 emps1_.DEPTNO as DEPTNO1_1_ 80 from 81 DEPT dept0_ 82 right outer join 83 EMP emps1_ 84 on dept0_.DEPTNO=emps1_.DEPTNO