一、 Spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:conf/db.properties"></property>
</bean>
<!-- 配置数据源DataSource-->
<bean id="dataSource_ssh" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--数据库连接参数 -->
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<!-- 其他参数配置 -->
<property name="acquireIncrement" value="2"></property>
<property name="maxIdleTime" value="10"></property>
<property name="maxPoolSize" value="30"></property>
<property name="minPoolSize" value="2"></property>
<property name="initialPoolSize" value="2"></property>
</bean>
<!-- 配置SessionFactory-->
<bean id="sessionFactory_ssh" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource_ssh">
<!--<reflocal="dataSource_ssh"/>
--></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:hbm</value>
</list>
</property>
</bean>
<!-- 配置HibernateTemplate-->
<bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory_ssh"></property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory_ssh"></property>
</bean>
<!-- 事务管理器的包装 -->
<tx:advice id="advice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="query*" isolation="READ_COMMITTED"rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="true"/>
<tx:method name="*" isolation="READ_COMMITTED" rollback-for="java.lang.Exception" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 将事务切入所有的service-->
<aop:config>
<aop:pointcut expression="execution(* service.*.*(..))" id="pc"/>
<aop:advisor advice-ref="advice" pointcut-ref="pc"/>
</aop:config>
<!-- 给baseDao注入template -->
<bean id="baseDao" class="dao.impl.BaseDaoImpl"abstract="true">
<property name="template" ref="template"></property>
</bean>
<!--引入其他配置文件 -->
<import resource="classpath:spring/applicationContext-user.xml"/>
<import resource="classpath:spring/applicationContext-interact.xml"/>
</beans>
注意点:1.一旦引入了spring的事务管理,则spring会为每个线程持有一个session,则
在sessionfactory的配置中:
<propkey="hibernate.current_session_context_class">thread</prop>可以省略
2.如果没有引入spring的事务管理,则要在一个线程中保持session唯一,则必须:
<propkey="hibernate.current_session_context_class">thread</prop>
因为一旦有如上的配置,则hibernate会保证线程内部session唯一
sessionFactory.getCurrentSession();
二、Hibernate.cfg.xml 主配置文件省略,映射文件照常写 ***.hbm.xml
(1). *spring+hibernate:
1.建立映射文件
2.将service、dao、sessionfactory、hibernatetemplate、datasource、事务
置入工厂,并通过IOC满足依赖关系,及通过aop将事务切入service
(2). *spring+strus2:
1.建立strus2配置文件,并导入前端控制器
2.在web.xml中声明监听器:
<!-- 在项目启动时,将加载spring工厂-->
3.在struts.xml中配置action时:
<!--
class:改为action的bean的id,如:
class="studentAction"
-->
<actionname="insert" class="studentAction"method="insertStudent">
<resultname="ok">/index.jsp</result>
<resultname="error1">/error.jsp</result>
</action>
4.导入spring+struts2整合jar:
struts2-spring-plugin-2.3.3.jar
5.将Action置入工厂,并IOC满足依赖
三、处理hibernate的懒加载
opensessioninview:osiv
<filter>
<filter-name>osiv34</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<!-- 将工厂中的sessionfactory的beanID交给此过滤器-->
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory34</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>osiv34</filter-name>
<!-- 过滤需要处理延迟加载属性的请求
不建议定义为/*
-->
<url-pattern>/user/query</url-pattern>
<url-pattern>/admin/query2</url-pattern>
<url-pattern>/order/queryOrder</url-pattern>
</filter-mapping>
被此过滤器过滤的请求,session会一直保持开启,知道View处理完毕时,才关闭session
四. Struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD StrutsConfiguration 2.1.7//EN" "struts-2.1.7.dtd">
<struts>
<!-- 用户信息action-->
<package name="user" namespace="/user"extends="struts-default">
<action name="user" class="userAction">
<result name="login" type="redirectAction">
<param name="namespace">/interact</param>
<param name="actionName">interact_queryAllInteract</param>
</result>
</action>
<action name="teacher"method="queryTeacher" class="userAction">
<result name="teacher">/jsp/queryTeacher.jsp</result>
</action>
</package>
<!-- 互动Action -->
<package name="interact" namespace="/interact"extends="struts-default">
<action name="interact_*" method="{1}" class="interactAction">
<!-- 分页查询 -->
<result name="all">/jsp/queryAllInteract.jsp</result>
<!-- 查询互动详情 -->
<result name="all2">/jsp/interactDetail.jsp</result>
</action>
</package>
</struts>
五. 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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 解决懒加载问题,通过openSessionInView过滤器 -->
<filter>
<filter-name>osiv</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory_ssh</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>osiv</filter-name>
<url-pattern>/user/teacher</url-pattern>
</filter-mapping>
<!-- spring对工厂的监听器 -->
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!-- struts2的前端控制器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>