1.配置spring 初始化监听器
<?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">
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com.book.manager.filter.CharsetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Verification</filter-name>
<filter-class>com.book.manager.filter.Verification</filter-class>
</filter>
<filter-mapping>
<filter-name>Verification</filter-name>
<url-pattern>/interceptor-jsp/*</url-pattern>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-ssh.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>
2.配置jdbc.properties
user=bookmanager
password=test
driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
3.配置spring-ssh.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:aop = "http://www.springframework.org/schema/aop"
xmlns:context = "http://www.springframework.org/schema/context"
xmlns:tx = "http://www.springframework.org/schema/tx"
xmlns:instance = "http://www.w3.org/2001/XMLSchema-instance"
instance:schemaLocation = "
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
"
>
<context:component-scan base-package="com.book.manager.*">
</context:component-scan>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" >
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClassName" value="${driver}"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>com/book/manager/entity/bookEntityOrm.xml</value>
<value>com/book/manager/entity/userEntityOrm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
<prop key="dialect">
org.hibernate.dialect.Oracle10gDialect
</prop>
</props>
</property>
</bean>
<bean id = "hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id = "transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdivse" transaction-manager="transactionManager" >
<tx:attributes >
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" timeout="2" rollback-for="java.lang.Exception" />
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" timeout="2" rollback-for="java.lang.Exception"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" timeout="2" rollback-for="java.lang.Exception"/>
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression ="execution(* com.book.manager.service.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref = "txAdivse" pointcut-ref="pointCut"/>
</aop:config>
</beans>
4.把管理权给予spring
<?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>
<constant name="struts.action.extension" value="action,do"/>
<constant name="struts.objectFactory" value="spring"></constant>
</struts>
5.所有DaoImpl继承 hibernateDaoSupper
package com.book.manager.dao
import javax.annotation.Resource
import org.apache.log4j.Category
import org.apache.log4j.Logger
import org.hibernate.Session
import org.hibernate.SessionFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.orm.hibernate4.HibernateTemplate
import org.springframework.orm.hibernate4.support.HibernateDaoSupport
import org.springframework.stereotype.Repository
// 所以 dao 层的实现类必须继承 hibernateDaoSupport
//这里继承SuperDao 就 可以通过getSeeion 来获取session
public class SuperDao extends HibernateDaoSupport {
@Resource(name="hibernateTemplate")
public void setChildHibernateTemplate(HibernateTemplate template){
logger.debug("----> " + template)
//设置父类的hibernateTemplate
super.setHibernateTemplate(template)
}
@Resource(name="sessionFactory")
public void setChildSessionFactory(SessionFactory sessionFactory){
//设置父类的sessionFactory
super.setSessionFactory(sessionFactory)
}
public Session getSession(){
logger.debug("----> "+this.getClass().getName())
logger.debug("----> 1"+getHibernateTemplate())
logger.debug("----> 2"+getHibernateTemplate().getSessionFactory())
logger.debug("----> 3"+getHibernateTemplate().getSessionFactory().getCurrentSession())
return getHibernateTemplate().getSessionFactory().getCurrentSession()
//return getSessionFactory().getCurrentSession()
}
}