首页 > 代码库 > Spring(五):Spring&Struts2&Hibernate整合后,实现查询Employee信息

Spring(五):Spring&Struts2&Hibernate整合后,实现查询Employee信息

  • 背景:

  基于之前两篇文章《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     }14 15     public String getDeparmentName() {16         return deparmentName;17     }18 19     public void setDeparmentName(String deparmentName) {20         this.deparmentName = deparmentName;21     }22 }
View Code

Employee.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.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>
View Code

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信息