SSH增删查改功能(人力资源管理系统)
一、准备工作
开发环境:MyEclipse6.0 +JDK6.0+Tomcat5.0+SQLServer
struts2+Spring2.0.6+Hibernate3.1。本示例通过对一个人力资源的系统,提供基本的增加、删除、修改、查询等功能。更详细请看附件:SSH增删查改详解。
二、建立数据库
三、建立数据化持久层
建立Hibernate逆向工程
1、建立实体类Employee及Employee.hbm.xml映射文件。
Employee.java
package com.fejd.companyrm.model;/** * Employee entity. * * @author MyEclipse Persistence Tools */public class Employee implements java.io.Serializable {private Long id;private String name;private String address;private String phone;// Constructors/** default constructor */public Employee() {}/** full constructor */public Employee(String name, String address, String phone) {this.name = name;this.address = address;this.phone = phone;}// Property accessorspublic Long getId() {return this.id;}public void setId(Long id) {this.id = id;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public String getAddress() {return this.address;}public void setAddress(String address) {this.address = address;}public String getPhone() {return this.phone;}public void setPhone(String phone) {this.phone = phone;}}2、把实体类Employee的属性映射到Employee表,生成下面的Employee.hbm.xml文件:<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping> <class name="com.fejd.companyrm.model.Employee" table="Employee" schema="dbo" catalog="CompanyRM"> <id name="id" type="java.lang.Long"> <column name="ID" /> <generator /> </id> <property name="name" type="java.lang.String"> <column name="Name" length="50" /> </property> <property name="address" type="java.lang.String"> <column name="Address" length="50" /> </property> <property name="phone" type="java.lang.String"> <column name="Phone" length="50" /> </property> </class></hibernate-mapping>
四、建立DAO层
1、建立DAO的接口类:EmployeeDaopackage com.fejd.companyrm.dao;import java.util.List;import com.fejd.companyrm.model.Employee;public interface EmployeeDao {public void add(Employee e);//添加新雇员public Employee getEmployee(Long id);//根据雇员的ID显示雇员的所有信息public List getEmployees();//显示所有雇员信息public void delete(Long id);//根据雇员ID删除雇员public void saveOrUpdate(Employee e);//根据雇员ID,修改或更新某个雇员信息public List getEmployee(String name,String phone);//判断是否为合法雇员}
2、实现此接口的类文件,EmployeeDaoImplpackage com.fejd.companyrm.dao.impl;import java.util.Iterator;import java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.fejd.companyrm.dao.EmployeeDao;import com.fejd.companyrm.model.Employee;public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {private SessionFactory sessionFactory;public SessionFactory getFactory() {return sessionFactory;//hibernate SessionFactory对象,由spring注入.}public void setFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;//get/set方法在spring注入时使用}public void add(Employee e) {/** * 函数说明:添加雇员 * 参数说明:对象 * 返回值: */this.getHibernateTemplate().save(e);}public void delete(Long id) {/** * 函数说明:删除雇员 * 参数说明: 对象 * 返回值: */this.getHibernateTemplate().delete(this.getEmployee(id));}public Employee getEmployee(Long id) { /** * 函数说明:获得一个雇员的所有信息 * 参数说明: ID * 返回值:对象 */return (Employee)this.getHibernateTemplate().get(Employee.class, id);}public List getEmployees() {/** * 函数说明:获得所有雇员的信息 * 参数说明: * 返回值:信息的集合 */return this.getHibernateTemplate().find("from Employee");}public List getEmployee(String name,String phone){Object o[]={name,phone};System.out.println("查询的结果是:"+name+phone);return this.getHibernateTemplate().find("select employee from Employee as employee where employee.name=? and employee.phone=?",o);}public void saveOrUpdate(Employee e) {/** * 函数说明:修改雇员信息 * 参数说明: 对象 * 返回值: */this.getHibernateTemplate().saveOrUpdate(e);}}
五、建立业务逻辑层
注明:在业务逻辑层需要认真思考每个业务逻辑所能用到的持久层对象和DAO。DAO层之上是业务逻辑层,DAO类可以有很多个,但业务逻辑类应该只有一个,可以在业务逻辑类中调用各个DAO类进行操作。
1、创建服务接口类EmployeeManagerpackage com.fejd.companyrm.service;import java.util.List;import com.fejd.companyrm.model.Employee;public interface EmployeeManager {public void addEmployee(Employee e);//增加雇员public List listEmployee();//获得雇员的集合public void deletEmployee(Long id);//删除雇员public void savaOrUpdate(Employee e);//修改雇员public Employee getEmployee(String name,String phone);//根据雇员的某个记录查询雇员信息public Employee ggetEmployee(Long id);//根据ID查询雇员,实现修改操作}
2、实现此接口类:EmployeeManagerImplpackage com.fejd.companyrm.service.impl;import java.util.List;import com.fejd.companyrm.dao.EmployeeDao;import com.fejd.companyrm.model.Employee;import com.fejd.companyrm.service.EmployeeManager;public class EmployeeManagerImpl implements EmployeeManager {private EmployeeDao employeeDao;public void setEmployeeDao(EmployeeDao employeeDao) {this.employeeDao = employeeDao;}public void addEmployee(Employee e) { /** * 函数说明:添加雇员 * 参数说明:对象 * 返回值: */this.employeeDao.add(e);}public void deletEmployee(Long id) {/** * 函数说明:删除雇员 * 参数说明: 对象 * 返回值: */this.employeeDao.delete(id);}public List listEmployee() { /** * 函数说明:获得所有雇员的信息 * 参数说明: * 返回值:信息的集合 */return this.employeeDao.getEmployees();}public Employee getEmployee(String name,String phone) {List list=employeeDao.getEmployee(name,phone);if(list!=null&&list.size()==1){System.out.println("查询的结果是:"+name+phone);return (Employee)list.get(0);}elsereturn null;}public void savaOrUpdate(Employee e) {/** * 函数说明:修改雇员信息 * 参数说明: 对象 * 返回值: */employeeDao.saveOrUpdate(e);}public Employee ggetEmployee(Long id) { /** * 函数说明:获得一个雇员的信息 * 参数说明: ID * 返回值:对象 */return employeeDao.getEmployee(id);}}
六、创建Action类:EmployeeAction.java:package com.fejd.companyrm.action;import java.util.List;import com.fejd.companyrm.model.Employee;import com.fejd.companyrm.service.EmployeeManager;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;public class Employee Action extends ActionSupport implements ModelDriven{private EmployeeManager employeeManager;private List employees;private Employee employee;private Long id;public void setEmployeeManager(EmployeeManager employeeManager) {this.employeeManager = employeeManager;}public String add(){employeeManager.addEmployee(employee);return SUCCESS;}public String list(){this.employees=employeeManager.listEmployee();return SUCCESS;}public String delete(){employeeManager.deletEmployee(id);return SUCCESS;}public String saveOrUpdate(){employeeManager.savaOrUpdate(employee);return SUCCESS;}public String edit(){employee=employeeManager.ggetEmployee(id);return SUCCESS;}public String save(){Long id=this.getEmployee().getId();employee=employeeManager.ggetEmployee(id);return SUCCESS;}public String update(){Long id=this.getEmployee().getId();employeeManager.savaOrUpdate(this.getEmployee());return SUCCESS;}public Employee getEmployee() {return employee;}public void setEmployee(Employee employee) {this.employee = employee;}public List getEmployees() {return employees;}public void setEmployees(List employees) {this.employees = employees;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public Employee getModel() {return employee;}}
七、Web页面
1、主页面:index.jsp,其代码如下<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><html> <head> </head> <body> <center><h1>人力资源管理系统</h1></center> <hr color="red"> <center> <a href="add.jsp">雇员登记</a> <a href="list.action">显示用户列表</a> </center> </body></html>
2、增加页面add.jsp:<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ taglib uri="/struts-tags" prefix="s" %><html> <head> <title>add.jsp</title> <!-- 增加雇员信息页面 --> </head> <body> 请输入雇员信息: <s:form action="add"> <s:textfield name="employee.name" label="姓名"></s:textfield> <s:textfield name="employee.address" label="地址"></s:textfield> <s:textfield name="employee.phone" label="电话"></s:textfield> <s:submit></s:submit> </s:form> </body></html>
3、列表页面:list.jsp<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ taglib uri="/struts-tags" prefix="s"%><html> <head> <title>list.jsp</title> <!-- 显示用户列表页面 --> </head> <body> 输出雇员的信息:<br /> <s:iterator value="employees"> <table> <tr><td>编号:</td><td><s:property value="id"/></td></tr> <tr><td>姓名:</td><td><s:property value="name" /></td></tr> <tr><td>地址:</td><td><s:property value="address"/></td></tr> <tr><td>电话:</td><td><s:property value="phone"/></td></tr> <tr><td align="center"> <a href="edit.action?id=<s:property value='id' /> ">更新</a></td> <td align="center"> <a href='<s:url action="delete"><s:param name="id" value="id"></s:param></s:url>'>删除</a> </td> </tr> </table> </s:iterator> </body></html>
注明:
(1)、<s:property> :得到'value'的属性,如果value没提供,默认为堆栈顶端的元素。其相关的参数及使用如下表所示:
名称必需默认类型描述
default否String如果属性是null则显示的default值
escape否trueBooelean是否escape HTML
value否栈顶Object要显示的值
id否Object/String用来标识元素的id。在UI和表单中为HTML的id属性
(2)、<s:Iterator>:用于遍历集合(java.util.Collection)或枚举值(java.util.Iterator)。其相关的参数及使用如下表所示:
名称必需默认类型描述
status否String如果设置此参数,一个IteratorStatus的实例将会压入每个遍历的堆栈
value否Object/String要遍历的可枚举的(iteratable)数据源,或者将放入新列表(List)的对象
id否Object/String用来标识元素的id。在UI和表单中为HTML的id属性
(3)、<s:param>:为其他标签提供参数,比如include标签和bean标签. 参数的name属性是可选的,如果提供,会调用Component的方法addParameter(String, Object), 如果不提供,则外层嵌套标签必须实现UnnamedParametric接口(如TextTag)。 value的提供有两种方式,通过value属性或者标签中间的text,不同之处我们看一下例子:
<param name="color">blue</param><!-- (A) -->
<param name="color" value="blue"/><!-- (B) -->
(A)参数值会以String的格式放入statck.
(B)该值会以java.lang.Object的格式放入statck.
4、修改页面:update.jsp<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%@ taglib uri="/struts-tags" prefix="s"%><html> <head> </head> <body> <s:form action="update.action" method="post"> <s:hidden name="employee.id"></s:hidden> <s:textfield name="employee.name" label="用户名"></s:textfield> <s:textfield name="employee.address" label="地址"></s:textfield> <s:textfield name="employee.phone" label="电话"></s:textfield> <s:submit></s:submit> </s:form> </body></html>
八、配置信息:
1、struts.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.objectFactory" value="spring" /> <package name="struts2" extends="struts-default"> <action name="add" method="add"> <result type="redirect">list.action</result> </action> <action name="list" method="list"> <result>/list.jsp</result> </action> <action name="delete" method="delete"> <result type="redirect">list.action</result> </action> <action name="update" method="saveOrUpdate"> <result type="redirect">list.action</result> <result name="input">/update.jsp</result> <result name="error">/error.jsp</result> </action> <action name="edit" method="edit"> <result>/update.jsp</result> </action> <action name="login" method="login"> <result>/add.jsp</result> <result name="error">/login.jsp</result> </action> </package></struts>
2、applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource"
value="sa"></property>
<property name="password" value="sa"></property>
</bean>
<bean id="sessionFactory"
/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/fejd/companyrm/model/Employee.hbm.xml</value></list>
</property></bean>
<bean id="employeeDao"
abstract="false"
lazy-init="default" autowire="default" dependency-check="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="employeeManager"
lazy-init="default" autowire="default"
dependency-check="default">
<property name="employeeDao">
<ref bean="employeeDao" />
</property>
</bean>
<bean id="addBean" />
</property>
</bean>
<bean id="deleteBean" />
</property>
</bean>
<bean id="listBean" />
</property>
</bean>
<bean id="updateBean" />
</property>
</bean>
<bean id="editBean" scope="prototype">
<property name="employeeManager">
<ref bean="employeeManager"/>
</property>
</bean>
<bean id="loginBean" />
</property>
</bean>
</beans>