SSH(Spring4+Struts2+Hibernate5整合)

首先准备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

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="entity">

	<class name="Dept" table="t_dept">
		<id name="id" column="deptId">
			<generator class="native"></generator>
		</id>
		<property name="name" column="deptName"></property>
	</class>
</hibernate-mapping>


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

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="entity">

	<class name="Employee" table="t_employee">
		<id name="id" column="empId">
			<generator class="native"></generator>
		</id>
		<property name="empName"></property>
		<property name="salary"></property>
		<!-- 多对一 -->
		<many-to-one name="dept" column="dept_id" class="Dept"></many-to-one>
	</class>

</hibernate-mapping>


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;
	}

	@Override
	public 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";
	}
}
 request = (Map) ActionContext.getContext().get("request");
		request.put("employee", employee);
		return "success";
	}
}


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">
	<display-name>SSH</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- 配置spring的OpenSessionInView模式 【目的:JSp页面访问懒加载数据】 -->
	<!-- 注意:访问struts时候需要带上*.action后缀,必须写在sturts的拦截器之前,否则会保错 -->
	<filter>
		<display-name>OpenSessionInView</display-name>
		<filter-name>OpenSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInView</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>
	<!-- struts2配置 -->
	<filter>
		<display-name>Struts2</display-name>
		<filter-name>Struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>Struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- Spring配置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:bean*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>
struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="emp" extends="struts-default">
		<!-- action实例交给spring容器创建 -->
		<action name="show" class="action.EmployeeAction" method="execute">
			<result name="success">/index.jsp</result>
		</action>
	</package>
</struts>
bean-base.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" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
	<!-- 所有配置的公共部门 -->

	<!-- 1) 连接池实例 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="org.gjt.mm.mysql.Driver"></property>
		<property name="jdbcUrl"
			value="jdbc:mysql:///hib_demo?characterEncoding=utf-8"></property>
		<property name="user" value="root"></property>
		<property name="password" value="(chaoxin)1997"></property>
		<property name="initialPoolSize" value="3"></property>
		<property name="maxPoolSize" value="10"></property>
		<property name="maxStatements" value="100"></property>
		<property name="acquireIncrement" value="2"></property>
	</bean>
	<!-- 2) SessionFactory实例创建 -->
	<!-- 所有的配置都由spring维护(项目中不需要hibernate.cfg.xml啦) -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
		<!-- a. 连接池 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- b. hibernate常用配置: 方言、显示sql、自动建表等 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<!-- c. 映射配置 -->
		<property name="mappingLocations">
			<list>
				<value>classpath:entity/*.hbm.xml</value>
			</list>
		</property>
	</bean>
	<!-- 3) 事务配置 -->
	<!-- # 事务管理器 -->
	<bean id="txManager"
		class="org.springframework.orm.hibernate5.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<!-- # 事务增强 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="*" read-only="false" />
		</tx:attributes>
	</tx:advice>
	<!-- # AOP配置 -->
	<aop:config>
		<aop:pointcut expression="execution(* service.*.*(..))"
			id="pt" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
	</aop:config>
</beans>
bean-dao.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" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
	<bean id="employeeDao" class="dao.EmployeeDao">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
</beans>

bean-service.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" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
	<bean id="employeeService" class="service.EmployeeService">
		<property name="employeeDao" ref="employeeDao"></property>
	</bean>
</beans>

bean-action.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" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
	<bean id="employeeAction" class="action.EmployeeAction" scope="prototype">
		<property name="employeeService" ref="employeeService"></property>
	</bean>
</beans>


index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!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>index</title>
</head>
<body>
	员工:${employee.empName }
	<br /> 部门:${employee.dept.name }
</body>
</html>

数据库结构如图

 

运行结果如图

Spring4+Struts2+Hibernate5整合结束

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值