SSH(Spring4+Struts2+Hibernate5整合)

来源:互联网 发布:怎么开通淘宝直播视频 编辑:程序博客网 时间:2024/05/21 06:35

首先准备jar包和配置文件搭建环境jar包和配置文件如图:

其分别在spring-framework-4.3.9.RELEASE-dist.zip和hibernate-release-5.2.10.Final.zip与struts-2.5.10.1-all.zip中获取


这里配置jar包的方法使用的是eclipse配置导入jar包这里可以参考我之前的文章里面有配置的具体步骤:

http://blog.csdn.NET/weixin_39135506/article/details/75095294

以下是我的目录结构如图:


代码如下:

Dept.java

package entity;public class Dept {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
Dept.hbm.xml

Employee.java

package entity;public class Employee {private int id;private String empName;private double salary;private Dept dept;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getEmpName() {return empName;}public void setEmpName(String empName) {this.empName = empName;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public Dept getDept() {return dept;}public void setDept(Dept dept) {this.dept = dept;}}
Employee.hbm.xml

EmployeeDao.java

package dao;import java.io.Serializable;import org.hibernate.SessionFactory;import entity.Employee;public class EmployeeDao {// 注入sessionFactory对象private SessionFactory sessionFactory;public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}/** * 保存员工 *  * @param employee */public Employee findById(Serializable id) {return sessionFactory.getCurrentSession().get(Employee.class, id);}}
EmployeeService.java

package service;import java.io.Serializable;import dao.EmployeeDao;import entity.Employee;public class EmployeeService {private EmployeeDao employeeDao;public void setEmployeeDao(EmployeeDao employeeDao) {this.employeeDao = employeeDao;}public Employee findById(Serializable id) {Employee employee = employeeDao.findById(id);return employee;}}
EmployeeAction.java
package action;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import entity.Employee;import service.EmployeeService;@SuppressWarnings("serial")public class EmployeeAction extends ActionSupport {private EmployeeService employeeService;public void setEmployeeService(EmployeeService employeeService) {this.employeeService = employeeService;}@Overridepublic String execute() {int empId = 2;Employee employee = employeeService.findById(empId);@SuppressWarnings("unchecked")Map request = (Map) ActionContext.getContext().get("request");request.put("employee", employee);return "success";}}

web.xml
SSHindex.htmlindex.htmindex.jspdefault.htmldefault.htmdefault.jspOpenSessionInViewOpenSessionInVieworg.springframework.orm.hibernate5.support.OpenSessionInViewFilterOpenSessionInView*.actionStruts2Struts2org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterStruts2/*contextConfigLocationclasspath:bean*.xmlorg.springframework.web.context.ContextLoaderListener
struts.xml
/index.jsp
bean-base.xml
org.hibernate.dialect.MySQLDialecttrueupdateclasspath:entity/*.hbm.xml
bean-dao.xml
bean-service.xml
bean-action.xml


index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>index员工:${employee.empName }
部门:${employee.dept.name }
数据库结构如图



运行结果如图


Spring4+Struts2+Hibernate5整合结束