视频地址: 基于SSH实现员工管理系统之案例实现篇
我的整个完整源码地址 : 点击进入 github
这是SSH 整合项目的第三章,第一章基于Meavn 整合 Spring 和 Hibernate 框架并进行了进行了自动建表,第二章则在此基础上加入 Struts 2 框架以及完成一个简单的登陆页面。第三章主要侧重于分页查询功能的实现,第四章则着重完善增删改查代码,因为重复性太大,直接将代码粘上来,比较难的会写上注释。
首先写Employee 的增删改查,直接写Action--->Service---->Dao
EmployeeAction.java 中新增的代码
/** * 首先获取到所有的部门,更改员工所在的部门,然后跳转到真正添加员工的jsp */ public String saveUI() { //查询部门 List<Department> list = departmentService.findAll(); //集合用 set 方法, 对象用 push ActionContext.getContext().getValueStack().set("list", list); return "saveUI"; } /** * 保存员工的 save() */ public String save() { employeeService.save(employee); return "saveSuccess"; } /** * 更新员工信息的方法 */ public String edit() { //根据员工ID 查询员工 employee = employeeService.findById(employee.getEid()); List<Department> list = departmentService.findAll(); ActionContext.getContext().getValueStack().set("list", list); return "editSuccess"; } public String update() { employeeService.update(employee); return "updateSuccess"; } /** * 删除员工使用的方法 */ public String delete(){ employeeService.delete(employee); return "deleteSuccess"; }
EmployeeService.java(Service 的接口全部代码)
package com.test.ssh.service; import com.test.ssh.domain.Employee; import com.test.ssh.domain.PageBean; public interface EmployeeService { public Employee login(Employee employee); PageBean<Employee> fingByPage(Integer currPage); void save(Employee employee); Employee findById(int eid); void delete(Employee employee); void update(Employee employee); }EmployeeServiceImpl.java 中新增代码
@Override public void save(Employee employee) { employeeDao.save(employee); } @Override public Employee findById(int eid) { return employeeDao.findById(eid); } @Override public void delete(Employee employee) { employeeDao.delete(employee); } @Override public void update(Employee employee) { employeeDao.update(employee); }
此时在/web/frame 下新增一个 editEmployee.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title></title> <body> <table border="0" width="600px"> <tr> <td align="center" style="font-size:24px; color:#666"> 员工修改</td> </tr> <tr> <td align="right"> <a href="javascript:document.getElementById('saveForm').submit()">保存</a> <a href="javascript:history.go(-1)">退回</a> </td> </tr> </table> <br/> <s:form action="employee_update" method="post" namespace="/" id="saveForm" theme="simple"> <s:hidden name="eid" value="%{model.eid}"/> <table border='0' cellpadding="0" cellspacing="10"> <tr> <td>姓名:</td> <td><s:textfield name="ename" value="%{model.ename}"/></td> <td>性别:</td> <td><s:radio name="sex" list="{'男','女'}" value="%{model.sex}"/></td> </tr> <tr> <td>出生日期:</td> <td><input type="text" name="birthday" value="<s:date name="model.birthday" format="yyyy-MM-dd"/>"/></td> <td>入职时间:</td> <td><input type="text" name="joinDate" value="<s:date name="model.joinDate" format="yyyy-MM-dd"/>"/></td> </tr> <tr> <td>所属部门:</td> <td><s:select name="department.did" list="list" listKey="did" listValue="dname" value="%{model.department.did}" headerKey="" headerValue="-----请--选--择----"/></td> <td>编号:</td> <td><s:textfield name="eno" value="%{model.eno}"/></td> </tr> <tr> <td>用户名:</td> <td><s:textfield name="username" value="%{model.username}"/></td> <td>密码:</td> <td><s:password name="password" value="%{model.password}" showPassword="true"/></td> </tr> </table> </s:form> </body> </html>
在同一位置新增一个editDepartment.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title></title> </head> <body> <table border="0" width="600px"> <tr> <td align="center" style="font-size:24px; color:#666"> 部门编辑</td> </tr> <tr> <td align="right"> <a href="javascript:document.getElementById('saveForm').submit()">保存</a> <a href="javascript:history.go(-1)">退回 </a> </td> </tr> </table> <br/> <br> <s:form id="saveForm" action="department_update.action" method="post" namespace="/" theme="simple"> <s:hidden name="did" value="%{model.did}"/> <table style="font-size::16px"> <tr> <td>部门名称:</td> <td><s:textfield name="dname" value="%{model.dname}"/></td> </tr> <tr> <td>部门介绍:</td> <td></td> </tr> <tr> <td width="10%"></td> <td> <s:textarea cols="50" rows="5" name="ddesc" value="%{model.ddesc}"/>"/> </td> </tr> </table> </s:form> </body> </html>
测试发现出现Bug,
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.test.ssh.domain.Department; nested exception is org.hibernate.TransientObjectException: object r
查询后更改applicationContext.xml
将
<!--开启注解式事务管理 --> <tx:annotation-driven transaction-manager="transactionManager"/>
改为
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="search*" read-only="false" /> </tx:attributes> </tx:advice>
然后更改DepartmentAction.java(直接放全部代码)
package com.test.ssh.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.test.ssh.domain.Department; import com.test.ssh.domain.PageBean; import com.test.ssh.service.DepartmentService; import com.test.ssh.service.impl.DepartmentServiceImpl; public class DepartmentAction extends ActionSupport implements ModelDriven<Department> { //模型使用的驱动 private Department department = new Department(); private DepartmentService departmentService; /** * 查询并设置页数的操作 * 先默认当前页面 currPage = 1 */ private Integer currPage = 1; public void setCurrPage(Integer currPage) { this.currPage = currPage; } public String findAll() { PageBean<Department> pageBean = departmentService.findByPage(currPage); //将pageBean 存入到值栈中 ActionContext.getContext().getValueStack().push(pageBean); return "findAll"; } //添加部门 public String save() { departmentService.save(department); return "saveSuccess"; } /** * 我们在findAll页面里点击了 编辑按钮后,调用edit 方法, * 主要是获取想要修改的部门的属性,并展现在网页中, * 待用户修改完成后,点击保存,则调用的是 update() 方法进行更新 */ //编辑部门的执行的方法 public String edit() { department = departmentService.findById(department.getDid()); return "editSuccess"; } //修改部门的执行的方法 public String update() { departmentService.update(department); return "updateSuccess"; } /** * 为了完成级联删除,即删除某一个部门,对应的所有员工都被删除 * 需要先查询,后删除 */ public String delete() { department = departmentService.findById(department.getDid()); departmentService.delete(department); return "deleteSuccess"; } public void setDepartment(Department department) { this.department = department; } public void setDepartmentService(DepartmentService departmentService) { this.departmentService = departmentService; } @Override public Department getModel() { return department; } }DepartmentServiceImpl.java(接口中的父方法手动补全)
package com.test.ssh.service.impl; import com.test.ssh.dao.DepartmentDao; import com.test.ssh.domain.Department; import com.test.ssh.domain.Employee; import com.test.ssh.domain.PageBean; import com.test.ssh.service.DepartmentService; import java.util.List; public class DepartmentServiceImpl implements DepartmentService { private DepartmentDao departmentDao; public void setDepartmentDao(DepartmentDao departmentDao) { this.departmentDao = departmentDao; } @Override public PageBean<Department> findByPage(Integer currPage) { PageBean<Department> departmentPageBean = new PageBean<Department>(); // 封装当前的页数 departmentPageBean.setCurrPage(currPage); //封装每页显示的记录数,默认为3 int pageSize = 3; departmentPageBean.setPageSize(pageSize); //封装总记录数,总记录数通过查询数据库获得 int totalCount = departmentDao.findCount(); departmentPageBean.setTotalCount(totalCount); //封装总页数 double tc = totalCount; Double num = Math.ceil(tc / pageSize); departmentPageBean.setTotalPage(num.intValue()); //封装每页显示的数据 int begin = (currPage - 1) * pageSize; List<Department> list = departmentDao.findByPage(begin, pageSize); departmentPageBean.setList(list); return departmentPageBean; } @Override public List<Department> findAll() { return departmentDao.findAll(); } @Override public void save(Department department) { departmentDao.save(department); } @Override public void update(Department department) { departmentDao.update(department); } @Override public void delete(Department department) { departmentDao.delete(department); } @Override public Department findById(int did) { return departmentDao.findById(did); } }
DepartmentDaoImpl.java(接口中的父方法手动补全)
package com.test.ssh.dao.impl; import com.test.ssh.dao.DepartmentDao; import com.test.ssh.dao.EmployeeDao; import com.test.ssh.domain.Department; import com.test.ssh.domain.Employee; import org.hibernate.criterion.DetachedCriteria; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import java.util.List; public class DepartmentDaoImpl extends HibernateDaoSupport implements DepartmentDao { @Override public int findCount() { //查询个数 String hql = "select count(*) from Department "; List<Long> list = this.getHibernateTemplate().find(hql); //如果个数 if (list.size() > 0) { return list.get(0).intValue(); //将Long 类型转为 int 类型后返回. } return 0; } @Override public List<Department> findByPage(int begin, int pageSize) { DetachedCriteria criteria = DetachedCriteria.forClass(Department.class); List<Department> list = this.getHibernateTemplate().findByCriteria(criteria,begin,pageSize); return list; } @Override public List<Department> findAll() { //查询到所有语句的 hql 语句。 List<Department> list = this.getHibernateTemplate().find("from Department "); return list; } @Override public void save(Department department) { this.getHibernateTemplate().save(department); } @Override public Department findById(int did) { return this.getHibernateTemplate().get(Department.class,did); } @Override public void update(Department department) { this.getHibernateTemplate().update(department); } @Override public void delete(Department department) { this.getHibernateTemplate().delete(department); } }
然后进行对addDepartment.jsp 进行更改。
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" %> <%@ taglib uri="/struts-tags" prefix="s" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <title></title> </head> <body> <table border="0" width="600px"> <tr> <td align="center" style="font-size:24px; color:#666"> 部门添加</td> </tr> <tr> <td align="right"> <a href="javascript:document.getElementById('saveForm').submit()">保存</a> <a href="javascript:history.go(-1)">退回 </a> </td> </tr> </table> <br/> <br> <s:form id="saveForm" action="department_save.action" method="post" namespace="/" theme="simple"> <table style="font-size::16px"> <tr> <td>部门名称:</td> <td><s:textfield name="dname"/></td> </tr> <tr> <td>部门介绍:</td> <td></td> </tr> <tr> <td width="10%"></td> <td> <s:textarea cols="50" rows="5" name="ddesc"/> </td> </tr> </table> </s:form> </body> </html>
至此,本项目全部结束。
视频地址: 基于SSH实现员工管理系统之案例实现篇
我的整个完整源码地址 : 点击进入 github
不清楚的可以留言询问。