一、SSH版本:
1.Struts-2.3.31
2.Spring-4.3.5
3.Hibernate-4.2.21
二、引入jar包:
1.必须在WEB-INF下添加jar包(其他无效)
2.spring、hibernate及struts2的核心jar包,包含两个重复的javassist.jar,保留高版本的即可
3.mysql以及数据库连接池的jar包
三、使用XML配置:
1.基础applicationContext.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: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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
</beans>
2.jdbc.properties
<!-- 数据库连接池的基本配置 -->
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/ssh
jdbc.username=root
jdbc.password=root
3.Spring整合Hibernate
<!-- 引入jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 以下数据源,二选一即可 -->
<!-- 注入dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 注入c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 注入Hibernate的SessionFactory对象 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 配置Hibernate的全局配置文件 -->
<!-- <property name="configLocations" value="classpath:hibernate.cfg.xml"/> -->
<!-- 不使用hibernate.cfg.xml,由spring配置hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 配置.hbm.xml的位置及名称,可以使用通配符 -->
<property name="mappingLocations" value="classpath:com/ssh/domain/*.hbm.xml"/>
<!-- 配置单独的.hbm.xml -->
<property name="mappingResources">
<list>
<value>com/ssh/domain/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 注入UserDAO -->
<bean id="userDao" class="com.ssh.dao.UserDAO">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
//Spring自动生成的是代理对象,实现了UserDao接口,不能强转为UserDaoImpl对象
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UesrDao dao = (UserDao) context.getBean("userDao");
//UesrDaoImpl dao = (UserDaoImpl) context.getBean("userDao");
<!-- 注入UserService -->
<bean id="userService" class="com.ssh.dao.UserService">
<property name="userDao" ref="userDao"/>
</bean>
<!-- 配置 Spring 的声明式事务,需要引入com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar包 -->
<!-- 1. 配置数据源DataSource(必须) -->
<!-- 2. 配置事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 3. 配置事务建议者(属性) -->
<tx:advice id="txAdivce" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 4. 配置AOP切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.ssh.service.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txAdivce" pointcut-ref="pointcut"/>
</aop:config>
<!-- 5. 不能在hibernate中配置<property name="current_session_context_class">thread</property>,否则Spring不会自动开启事务 -->
4.Spring整合Struts2
首先需要引入Struts2下的spring插件包:struts2-spring-plugin-2.3.31.jar
配置web.xml:
<!-- 配置sprin监听器,在服务器启动时初始化IoC容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 默认寻找WEB-INF下的applicationContext.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!-- 配置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>
在applicationContext.xml中注入Action
//创建一个SuperActon继承ActionSupport并实现Web元素相关的接口
<bean id="superAction" class="com.ssh.action.SuperAction" />
<!-- 必须声明 scope 属性为 prototype (非单例) -->
//创建自己的Action继承SuperAction便可以直接使用request,session等对象
<bean id="userAction" class="com.ssh.action.UserAction" parent="superAction" scope="prototype">
<property name="userService" ref="userService"/>
</bean>
配置struts.xml
<!-- class属性的值对应beans.xml中注入的Action的id名 -->
<action name="user_*" class="userAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
四、使用注解+XML配置:
以下仅为不同于XML的配置
配置struts.xml文件时action的class值要写成Action类的全名
配置 applicationContext.xml:
<!-- 配置dataSource和sessionFactory -->
<!-- 注解映射类的包扫描器 -->
<property name="packagesToScan" value="com.ssh.entity"></property>
<!-- 开启注解式依赖与注入 -->
<context:component-scan base-package="com.ssh"/>
<!-- <context:annotation-config /> -->
<!-- 开启注解式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
注解类的配置:
@Controller
@Scope("prototype")
public class UserAction extends ActionSupport {
@Resource
private UserService userService;
}
@Service
@Transactional
public class UserService {
@Resource
private UserDaoImpl userDao;
}
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Resource
private SessionFactory sessionFactory;
}
//若使用继承HibernateDaoSupport的方式,则不能直接注入SessionFactory
@Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
@Resource
public void setSuperSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
}
完!