1. 导入相关jar,包含Spring/hibernate/struts
2. 配置web.xml
2.1 配置struts核心过滤器
<span style="white-space:pre"> </span><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>
2.2 配置Spring监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2.3 加载Spring配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
3.编写Hibernate.cfg.xml全局配置文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/bjsxt/smallming/entity/Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.编写XXX.hbm.xml映射文件
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.bjsxt.smallming.entity.Users">
<id name="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">seq_users</param>
</generator>
</id>
<property name="name" type="java.lang.String"></property>
<property name="address" type="java.lang.String"></property>
<property name="phone" type="java.lang.String"></property>
</class>
</hibernate-mapping>
5.编写applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!-- 整合Hibernate -->
<!-- 创建SessionFactory的Bean -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 创建Hibernate事务管理类 -->
<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>
<!-- 声明service.impl.X 下哪些方法被事务管理 -->
<!-- 设置当前方法和事务之间的关系
REQUIRED:如果当前已经有事务,使用当前事务,如果没有,创建事务。默认值
SUUPORTS: 如果当前已经有事务,使用当前事务,如果没有,则不使用事务
MANDATORY:必须有有事务,没有事务报异常
NEVER:必须没有事务,有的话报异常
NOT_SUPPORTED:要求执行方法时没有事务,如果有事务,挂起
REQUIRES_NEW:每次执行这个方法都创建新事务,如果已经有事物,创建嵌套子事务
read-only:true 当前方法只能读取,没有提交事务
-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP,让某些方法与声明式事务进行关联 -->
<aop:config>
<aop:pointcut expression="execution(* com.bjsxt.smallming.service.impl.*.*(..))" id="mypoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
</aop:config>
</beans>
6. 新建Dao和DaoImpl
public class UsersDaoImpl extends HibernateDaoSupport implements UsersDao{
public void insUsers(Users users) {
getHibernateTemplate().save(users);
}
}
7.新建Service和ServiceImpl
public class UsersServiceImpl implements UsersService{
private UsersDao usersDao;
public UsersDao getUsersDao() {
return usersDao;
}
public void setUsersDao(UsersDao usersDao) {
this.usersDao = usersDao;
}
@Override
public void insUsers(Users users) {
usersDao.insUsers(users);
}
}
8新建Action
9.配置struts.xml,class引用Spring配置文件中Action类的<bean>的id
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="Users_*" class="usersAction" method="{1}">
<result>/main.jsp</result>
</action>
</package>
</struts>
10.对Dao,Service,Action配置<bean>
<!-- 所有对象都由spring进行管理,依赖对象进行注入 -->
<bean id="userDao" class="com.bjsxt.smallming.dao.impl.UsersDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="usersService" class="com.bjsxt.smallming.service.impl.UsersServiceImpl">
<property name="usersDao" ref="userDao"></property>
</bean>
<!-- 修改Struts.xml中<action>class属性为下面<bean>的id -->
<bean id="usersAction" class="com.bjsxt.smallming.action.UsersAction">
<property name="usersService" ref="usersService"></property>
</bean>
.