SSH整合:
Spring与Hibernate整合
Spring与Struts整合
整合步骤:---------------------------------------------->本人使用的是struts2.3.4.1 hibernate3.6.0 spring3.2.5
1.导入jar文件
1)struts jar文件-->如何找?
-->去源码包中struts-2.3.4.1\apps\struts-blank.war
-->使用压缩文件打开struts-blank.war-->WEB-INF\lib(struts2-blank.war\WEB-INF\lib)
2)Hibernate jar文件 -->如何找?
-->hibernate源码中的hibernate-distribution-3.6.0.Final核心包 以及 hibernate-distribution-3.6.0.Final\lib包下的required和jpa文件下的所有包
3)spring jar包 -->如何找
-->spring核心包spring-framework-3.2.5.RELEASE\libs中找到四个核心包外加一个common-logging包(需要自己引入)
-->spring AOP包
aspectj aop优秀组件)
spring-aop-3.2.5.RELEASE.jar 【spring3.2源码】
aopalliance.jar 【spring2.5源码/lib/aopalliance】
aspectjweaver.jar 【spring2.5源码/lib/aspectj】或【aspectj-1.8.2\lib】
aspectjrt.jar 【spring2.5源码/lib/aspectj】或【aspectj-1.8.2\lib】
-->spring与struts整合的包
-->spring与Hibernate整合的包
建议最好对这些源码中进行分类总结--->可以在以后用到的时候不用找.直接拿来用即可为了开方便 可以使用用户库
案例整合SSH:
视图如下:
1.实体类
Dept
<span style="font-family:Courier New;font-size:14px;">package cn.itcast.domain;
import java.util.Set;
public class Dept {
private int id;
private String name;
private Set<Employee> employees;
//set get
}
</span>
Employee
<span style="font-family:Courier New;font-size:14px;">package cn.itcast.domain;
public class Employee {
private int id;
private String empName;
private Dept dept;
//get set
}
</span>
2.映射文件的配置
Dept.hbm.xml
<span style="font-family:Courier New;font-size:14px;"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.domain" auto-import="true">
<class name="Dept" table="t_dept" lazy="false">
<id name="id" column="deptId">
<generator class="native"></generator>
</id>
<property name="name" column="deptName"></property>
<!-- 一对多的关系映射 -->
<set name="employees">
<key column="dept_id"></key>
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping></span>
Employee.hbm.xml
<span style="font-family:Courier New;font-size:14px;"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.itcast.domain" auto-import="true">
<class name="Employee" table="t_employee">
<id name="id" column="empId">
<generator class="native"></generator>
</id>
<property name="empName" column="empName"></property>
<!-- 多对一的映射 -->
<many-to-one name="dept" column="dept_id"></many-to-one>
</class>
</hibernate-mapping></span>
3.EmployeeDao(sessionFactory注入)使用set方式
<span style="font-family:Courier New;font-size:14px;">package cn.itcast.dao;
import java.io.Serializable;
import org.hibernate.SessionFactory;
import cn.itcast.domain.Dept;
import cn.itcast.domain.Employee;
public class EmployeeDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Employee findById(Serializable id){
Employee emp=(Employee) sessionFactory.getCurrentSession().get(Employee.class, id);
// emp.getDept().getEmployees();为了解决懒加载需要用一下 因为session关闭之后 就不能使用数据了.
return emp;
}
}</span>
4.EmployeeService
<span style="font-family:Courier New;font-size:14px;">package cn.itcast.service;
import java.io.Serializable;
import cn.itcast.dao.EmployeeDao;
import cn.itcast.domain.Employee;
public class EmployeeService {
private EmployeeDao employeeDao;
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
public Employee findById(Serializable id){
Employee emp= employeeDao.findById(id);
return emp;
}
}
</span>
5.EmployeeAction
从数据库中查询员工 放到request域中 请求转发到主页
<span style="font-family:Courier New;font-size:14px;">package cn.itcast.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import cn.itcast.domain.Employee;
import cn.itcast.service.EmployeeService;
public class EmployeeAction extends ActionSupport{
private EmployeeService employeeService;
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
public String execute(){
Employee employee = employeeService.findById(4);
Map<String ,Object> emps= (Map<String, Object>) ActionContext.getContext().get("request");
emps.put("emp", employee);
return SUCCESS;
}
}
</span>
6.index.jsp 使用EL表达式方式获取数据
<span style="font-family:Courier New;font-size:14px;"> <body>
员工信息:${emp.empName }
部门信息:${emp.dept.name }
</body></span>
7.struts.xml文件的配置
<span style="font-family:Courier New;font-size:14px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="emp" extends="struts-default">
<action name="emp" class="employeeAction" >
<result name="success">/index.jsp</result>
</action>
</package>
</struts></span>
8.web.xml
<span style="font-family:Courier New;font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<!--1. struts过滤器的配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- Spring配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/bean*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
</span>
9.接下来进行spring的配置
1)bean-base.xml 公用的配置文件 数据库 sessionFactory 事务
<span style="font-family:Courier New;font-size:14px;"><?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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.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="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///day15"></property>
<property name="user" value="root"></property>
<property name="password" value="169500"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="5"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<!-- 2)sessionFactory的配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- a)数据源的配置 -->
<property name="dataSource" ref="dataSource"></property>
<!-- b)hibernate配置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:cn/itcast/domain/*.hbm.xml</value>
</list>
</property>
</bean>
<!-- 3)事务的配置 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.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(* cn.itcast.service.*.*(..))" id="pc"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
</aop:config>
</beans></span>
2)bean-action.xml
<span style="font-family:Courier New;font-size:14px;"><?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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<bean id="employeeAction" class="cn.itcast.action.EmployeeAction" scope="prototype">
<property name="employeeService" ref="employeeService"></property>
</bean>
</beans></span>
3)bean-service.xml
<span style="font-family:Courier New;font-size:14px;"><?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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<bean id="employeeService" class="cn.itcast.service.EmployeeService">
<property name="employeeDao" ref="employeeDao"></property>
</bean>
</beans></span>
4)bean.dao.xml
<span style="font-family:Courier New;font-size:14px;"><?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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<bean id="employeeDao" class="cn.itcast.dao.EmployeeDao">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans></span>
测试:发布到tomcat 访问
<img src="https://img-blog.csdn.net/20150731213459467?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
解释:为什么可以通过员工信息获取部门信息呢?spring管理事务实在service层 当service结束 事务就结束..而懒加载数据是不能再session关闭后访问的:
如果不设置下面的四种情况的任何一种配置 访问会出现错误!!!!!下面就是解决懒加载的方式
如何解决懒加载jsp访问数据:
如何解决session关闭后不能使用懒加载数据的问题?
// 方式1:先使用一下数据(在dao层使用一下数据 就解决了懒加载不能使用session关闭的数据)
// 方式2:强迫代理对象初始化
Hibernate.initialize(dept);
// 方式3:关闭懒加载 这里我使用的是这种方式
设置lazy=false;
/ 方式4:在使用数据之后,再关闭session!
spring 提供了一个过滤器解决懒加载问题!只需要在web.xml中进行配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置spring的OpenSessionInView模式 【目的:JSp页面访问懒加载数据】 -->
<!-- 注意:访问struts时候需要带上*.action后缀 -->
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>