还是接着上一篇继续。
web层实现
1、web.xml文件
ssh_employee
index.jsp
openSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
openSessionInViewFilter
*.action
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
2、Action
DepartmentAction类
package com.employee.action;
import com.employee.domain.Department;
import com.employee.domain.PageBean;
import com.employee.service.DepartmentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 部门action
* @author Administrator
* @date 2016年12月24日
*/
@SuppressWarnings("serial")
public class DepartmentAction extends ActionSupport implements ModelDriven{
//struts结合spring自动注入的department
private Department department = new Department();
public Department getModel() {
return department;
}
//spring注入departmentService
private DepartmentService departmentService;
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
//接收当前页面的值
private int currentPage = 1;
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
//分页查询部门
public String findAll(){
PageBean pageBean = departmentService.findByPage(currentPage);
//将pageBean存入到值栈中
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
//通过action返回添加部门的视图
public String saveUI(){
return "saveUI";
}
/**
* 保存部门
* @return
*/
//添加部门的save方法
public String save(){
departmentService.save(department);
return "saveSuccess";
}
/**
* 根据ID查询部门
* @return
*/
public String editById(){
department = departmentService.editById(department.getDid());
return "editSuccess";
}
/**
* 修改部门
* @return
*/
public String update(){
departmentService.update(department);
return "updateSuccess";
}
/**
* 删除部门
* @return
*/
public String delete(){
Department dep = departmentService.editById(department.getDid());
departmentService.delete(dep);
return "deleteSuccess";
}
}
EmployeeAction类
package com.employee.action;
import java.util.List;
import com.employee.domain.Department;
import com.employee.domain.Employee;
import com.employee.domain.PageBean;
import com.employee.service.DepartmentService;
import com.employee.service.EmployeeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 员工管理的Action类
* @author Administrator
* @date 2016年12月24日
*/
@SuppressWarnings("serial")
public class EmployeeAction extends ActionSupport implements ModelDriven{
//是struts与spring融合后自动注入的
private Employee employee = new Employee();
@Override
public Employee getModel() {
return employee;
}
//使用spring将service注入
private EmployeeService employeeService;
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
//spring注入部门管理的service
private DepartmentService departmentService;
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
//获取当前页
private int currentPage=1;
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* 登录执行的方法
* @return
*/
public String login(){
//调用业务层的类
Employee exsitEmploy = employeeService.findUserExist(employee);
if(exsitEmploy == null){
//登录失败
this.addActionError("用户名或者密码错误!");
return INPUT;
}else{
//登录成功
//把用户信息保存在Session域中
ActionContext.getContext().getSession().put("exsitEmploy",exsitEmploy);
return SUCCESS;
}
}
/**
* 分页查询员工
* @return
*/
public String findAll(){
PageBean pageBean = employeeService.findAll(currentPage);
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
/**
* 跳转到添加员工页面
* @return
*/
public String saveUI(){
//用于查询出所有的部门信息
List list = departmentService.findAll();
//保存到值栈中
ActionContext.getContext().getValueStack().set("list",list);
return "saveUI";
}
/**
* 保存员工
* @return
*/
public String save(){
employeeService.save(employee);
return "saveSuccess";
}
/**
* 编辑员工
* @return
*/
public String edit(){
employee = employeeService.edit(employee.getEid());
List list = departmentService.findAll();
ActionContext.getContext().getValueStack().set("list",list);
return "editSuccess";
}
/**
* 修改员工
* @return
*/
public String update(){
employeeService.update(employee);
return "updateSuccess";
}
/**
* 删除员工
* @return
*/
public String delete(){
Employee emp = employeeService.edit(employee.getEid());
employeeService.delete(emp);
return "deleteSuccess";
}
}
3、struts.xml
/index.jsp
/frame.jsp
/jsp/employee/listEmployee.jsp
/jsp/employee/addEmployee.jsp
employee_findAll.action
/jsp/employee/editEmployee.jsp
employee_findAll.action
employee_findAll.action
/jsp/department/listDepartment.jsp
/jsp/department/addDepartment.jsp
department_findAll.action
/jsp/department/editDepartment.jsp
department_findAll.action
department_findAll.action
service实现
1、接口
DepartmentService.java
package com.employee.service;
import java.util.List;
import com.employee.domain.Department;
import com.employee.domain.PageBean;
/**
* 部门管理的service接口
* @author Administrator
* @date 2016年12月24日
*/
public interface DepartmentService {
PageBean findByPage(int currentPage);
void save(Department department);
Department editById(Integer did);
void update(Department department);
void delete(Department department);
List findAll();
}
EmployeeService.java
package com.employee.service;
import com.employee.domain.Employee;
import com.employee.domain.PageBean;
/**
* 员工管理的service接口
* @author Administrator
* @date 2016年12月24日
*/
public interface EmployeeService {
Employee findUserExist(Employee employee);
PageBean findAll(int currentPage);
void save(Employee employee);
Employee edit(Integer eid);
void update(Employee employee);
void delete(Employee employee);
}
2、实现类
DepartmentServiceImpl.java
package com.employee.service.impl;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.employee.dao.DepartmentDao;
import com.employee.domain.Department;
import com.employee.domain.PageBean;
import com.employee.service.DepartmentService;
import com.opensymphony.xwork2.ActionContext;
/**
* 部门的service接口的实现类
* @author Administrator
* @date 2016年12月24日
*/
@Transactional
public class DepartmentServiceImpl implements DepartmentService {
//spring注入部门的departmentDaoImpl
private DepartmentDao departmentDao;
public void setDepartmentDao(DepartmentDao departmentDao) {
this.departmentDao = departmentDao;
}
/**
* 分页查询部门方法
*/
@Override
public PageBean findByPage(int currentPage) {
PageBean pageBean = new PageBean();
//封装当前页数
pageBean.setCurrentPage(currentPage);
//封装每页显示记录数
int pageCount = 3;
pageBean.setPageCount(pageCount);
//封装总记录数
int totalSize = departmentDao.findCount();
pageBean.setTotalSize(totalSize);
//封装总页数
double ts = totalSize;
Double num = Math.ceil(ts / pageCount);
pageBean.setPageSize(num.intValue());
//封装每页显示的数据
int begin = (currentPage - 1) * pageCount;
List list = departmentDao.findByPage(begin,pageCount);
pageBean.setList(list);
ActionContext.getContext().put("list", list);
return pageBean;
}
/**
* 保存部门
*/
@Override
public void save(Department department) {
departmentDao.save(department);
}
/**
* 根据ID查询部门
*/
@Override
public Department editById(Integer did) {
return departmentDao.editById(did);
}
/**
* 修改部门
*/
@Override
public void update(Department department) {
departmentDao.update(department);
}
/**
* 删除部门
*/
@Override
public void delete(Department dep) {
departmentDao.delete(dep);
}
/**
* 查询所有部门
*/
@Override
public List findAll() {
return departmentDao.findAll();
}
}
EmployeeServiceImpl.java
package com.employee.service.impl;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.employee.dao.EmployeeDao;
import com.employee.domain.Employee;
import com.employee.domain.PageBean;
import com.employee.service.EmployeeService;
/**
* 员工管理的service接口的实现类
* @author Administrator
* @date 2016年12月24日
*/
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
//使用spring注入的dao
private EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
/**
* 业务层的登录的执行方法
* @param employee
* @return
*/
@Override
public Employee findUserExist(Employee employee) {
return employeeDao.findUserExist(employee);
}
/**
* 分页查询员工
*/
@Override
public PageBean findAll(int currentPage) {
PageBean pageBean = new PageBean();
// 封装当前页数
pageBean.setCurrentPage(currentPage);
// 封装每页显示记录数
int pageCount = 3;
pageBean.setPageCount(pageCount);
// 封装总记录数
int totalSize = employeeDao.findCount();
pageBean.setTotalSize(totalSize);
// 封装总页数
double ts = totalSize;
Double num = Math.ceil(ts / pageCount);
pageBean.setPageSize(num.intValue());
// 封装每页显示的数据
int begin = (currentPage - 1) * pageCount;
// 这个方法用于查找出指定页数中的List
List list = employeeDao.findAll(begin,pageCount);
pageBean.setList(list);
return pageBean;
}
/**
* 保存员工
*/
@Override
public void save(Employee employee) {
employeeDao.save(employee);
}
/**
* 编辑员工
*/
@Override
public Employee edit(Integer eid) {
return employeeDao.edit(eid);
}
/**
* 修改员工
*/
@Override
public void update(Employee employee) {
employeeDao.update(employee);
}
/**
* 删除员工
*/
@Override
public void delete(Employee employee) {
employeeDao.delete(employee);
}
}
dao层实现
1、接口
DepartmentDao.java
package com.employee.dao;
import java.util.List;
import com.employee.domain.Department;
/**
* 员工管理的dao接口
* @author Administrator
* @date 2016年12月24日
*/
public interface DepartmentDao {
int findCount();
List findByPage(int begin, int pageSize);
void save(Department department);
Department editById(Integer did);
void update(Department department);
void delete(Department department);
List findAll();
}
EmployeeDao.java
package com.employee.dao;
import java.util.List;
import com.employee.domain.Employee;
/**
* 员工管理的dao接口
* @author Administrator
* @date 2016年12月24日
*/
public interface EmployeeDao {
Employee findUserExist(Employee employee);
int findCount();
List findAll(int begin, int pageSize);
void save(Employee employee);
Employee edit(Integer eid);
void update(Employee employee);
void delete(Employee employee);
}
2、实现类
DepartmentDaoImpl.java
package com.employee.dao.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.employee.dao.DepartmentDao;
import com.employee.domain.Department;
/**
* 部门的dao接口的实现类
* @author Administrator
* @date 2016年12月24日
*/
public class DepartmentDaoImpl extends HibernateDaoSupport implements DepartmentDao {
@SuppressWarnings("unchecked")
@Override
public int findCount() {
String hql = "select count(*) from Department";
List list = this.getHibernateTemplate().find(hql);
if(list.size()>0){
return list.get(0).intValue();
}else{
return 0;
}
}
@SuppressWarnings("unchecked")
@Override
public List findByPage(int begin, int pageCount) {
//通过分离标准方法得到数据
DetachedCriteria criteria = DetachedCriteria.forClass(Department.class);
List list = this.getHibernateTemplate().findByCriteria(criteria,begin,pageCount);
return list;
}
/**
* 保存部门
*/
@Override
public void save(Department department) {
this.getHibernateTemplate().save(department);
}
/**
* 根据ID查询部门
*/
@Override
public Department editById(Integer 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);
}
/**
* 查询所有部门
*/
@SuppressWarnings("unchecked")
@Override
public List findAll() {
String hql = "from Department";
List list = this.getHibernateTemplate().find(hql);
return list;
}
}
EmployeeDaoImpl.java
package com.employee.dao.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.employee.dao.EmployeeDao;
import com.employee.domain.Employee;
/**
* 员工管理的dao接口的实现类
* @author Administrator
* @date 2016年12月24日
*/
public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {
/**
* 查询用户名和密码是否存在
*/
@SuppressWarnings("unchecked")
@Override
public Employee findUserExist(Employee employee) {
String hql = " from Employee where username = ? and password = ?";
List list = this.getHibernateTemplate().find(hql,employee.getUsername(),employee.getPassword());
if(list.size() > 0){
return list.get(0);
}
return null;
}
/**
* 查询总记录数
*/
@SuppressWarnings("unchecked")
@Override
public int findCount() {
String hql = "select count(*) from Employee";
List list = this.getHibernateTemplate().find(hql);
if(list.size() > 0){
return list.get(0).intValue();
}
return 0;
}
/**
* 分页查询员工
*/
@SuppressWarnings("unchecked")
@Override
public List findAll(int begin, int pageCount) {
DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class);
List list = this.getHibernateTemplate().findByCriteria(criteria, begin, pageCount);
return list;
}
/**
* 保存员工
*/
@Override
public void save(Employee employee) {
this.getHibernateTemplate().save(employee);
}
/**
* 编辑员工
*/
@Override
public Employee edit(Integer eid) {
return this.getHibernateTemplate().get(Employee.class, eid);
}
/**
* 修改员工
*/
@Override
public void update(Employee employee) {
this.getHibernateTemplate().update(employee);
}
/**
* 删除员工
*/
@Override
public void delete(Employee employee) {
this.getHibernateTemplate().delete(employee);
}
}
配置文件
ApplicationContext.xml
org.hibernate.dialect.MySQLDialect
true
true
update
com/employee/domain/Department.hbm.xml
com/employee/domain/Employee.hbm.xml
jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssh
jdbc.username=root
jdbc.password=root
log4j.properties
log4j.rootLogger = debug, console, E
#level=INFO,all can be output
#console is set to be a ConsoleAppender
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = [%p] %d{yyyy-MM-dd HH:mm:ss} - %m%n
#file is set to output to a extra file
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.Threshold=error
log4j.appender.E.Encoding=UTF-8
log4j.appender.E.layout.ConversionPattern=[%p] %d{yyyy-MM-dd HH:mm:ss} - %m%n
log4j.appender.E.DatePattern='.'yyyy-MM-dd
log4j.appender.E.File = ${catalina.base}/logs/dd/error.log
存在BUG
在员工一端设置了保存更新级联属性,但是好像不起作用,一直没有找到问题出在哪里。而在部门一端设置的级
联删除还是起作用的。
项目源码下载地址:http://download.csdn.net/detail/erlian1992/9735699