Spring(五):Spring&Struts2&Hibernate整合後,實現查詢Employee信息
阿新 • • 發佈:2017-05-09
view event last .html ssh tla url 配置文件 hid
- 背景:
基於之前兩篇文章《Spring(三):Spring整合Hibernate》、《Spring(四):Spring整合Hibernate,之後整合Struts2》,了解了如何整合SSH的過程,但還不知道整合後在項目中該怎麽開發使用,本文主要講解的是基於SSH實現Employee信息查詢功能的使用。
- 新建Employee,Department實體類,並添加對應類的hibernate實體配置文件
新建包com.dx.ssh.entities,在該包下創建Employee、Department實體類,並添加對應的hibernate實體配置文件Employee.hbm.xml、Department.hbm.xml
Employee.java
1 package com.dx.ssh.entities; 2 3 public class Department { 4 private Integer id; 5 private String deparmentName; 6 7 public Integer getId() { 8 return id; 9 } 10 11 public void setId(Integer id) { 12 this.id = id; 13 }View Code14 15 public String getDeparmentName() { 16 return deparmentName; 17 } 18 19 public void setDeparmentName(String deparmentName) { 20 this.deparmentName = deparmentName; 21 } 22 }
Employee.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"View Code3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2017-5-9 16:33:32 by Hibernate Tools 3.5.0.Final --> 5 <hibernate-mapping> 6 <class name="com.dx.ssh.entities.Department" table="SSH_DEPARTMENT"> 7 <id name="id" type="java.lang.Integer"> 8 <column name="ID" /> 9 <generator class="native" /> 10 </id> 11 <property name="deparmentName" type="java.lang.String"> 12 <column name="DEPARMENTNAME" /> 13 </property> 14 </class> 15 </hibernate-mapping>
Department.java
1 package com.dx.ssh.entities; 2 3 import java.util.Date; 4 5 public class Employee { 6 private Integer id; 7 private String lastName; 8 private String email; 9 private Date birth; 10 // 不能被修改 11 private Date createTime; 12 // n:1的關系 13 private Department department; 14 15 public Integer getId() { 16 return id; 17 } 18 19 public void setId(Integer id) { 20 this.id = id; 21 } 22 23 public String getLastName() { 24 return lastName; 25 } 26 27 public void setLastName(String lastName) { 28 this.lastName = lastName; 29 } 30 31 public String getEmail() { 32 return email; 33 } 34 35 public void setEmail(String email) { 36 this.email = email; 37 } 38 39 public Date getBirth() { 40 return birth; 41 } 42 43 public void setBirth(Date birth) { 44 this.birth = birth; 45 } 46 47 public Date getCreateTime() { 48 return createTime; 49 } 50 51 public void setCreateTime(Date createTime) { 52 this.createTime = createTime; 53 } 54 55 public Department getDepartment() { 56 return department; 57 } 58 59 public void setDepartment(Department department) { 60 this.department = department; 61 } 62 }View Code
Department.hbm.xml
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2017-5-9 16:33:32 by Hibernate Tools 3.5.0.Final --> 5 <hibernate-mapping> 6 <class name="com.dx.ssh.entities.Employee" table="SSH_EMPLOYEE"> 7 <id name="id" type="java.lang.Integer"> 8 <column name="ID" /> 9 <generator class="native" /> 10 </id> 11 <property name="lastName" type="java.lang.String"> 12 <column name="LASTNAME" /> 13 </property> 14 <property name="email" type="java.lang.String"> 15 <column name="EMAIL" /> 16 </property> 17 <property name="birth" type="java.util.Date"> 18 <column name="BIRTH" /> 19 </property> 20 <property name="createTime" type="java.util.Date"> 21 <column name="CREATETIME" /> 22 </property> 23 <!-- 單項N:1關聯關系 --> 24 <many-to-one name="department" class="com.dx.ssh.entities.Department"> 25 <column name="DEPARTMENT_ID" /> 26 </many-to-one> 27 </class> 28 </hibernate-mapping>View Code
Spring(五):Spring&Struts2&Hibernate整合後,實現查詢Employee信息