一:整合的步骤:
第一步:导入S2SH框架所需jar包
第二步:配置文件,整备:applicationContext.xml、hibernate.cfg.xml、 *.hbm.xml、 struts.xml
第三步:web.xml加入Spring的监听器、Struts2的核心过滤器
web.xml配置如下:
<span style="font-size:14px;"><span style="white-space:pre"> </span><span style="font-size:14px;"><!-- 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>
<!-- 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>/*</url-pattern>
</filter-mapping></span></span>
第四步:编写实体类、dao层、Service层、Control层
第五步:编写相关框架的配置文件
第六步:编写视图层页面
以上步骤纯粹是个人偏好这样写的。也可以按照其他步骤的。
二:1、实体类及映射文件代码如下:
<span style="font-size:14px;"><span style="font-size:14px;">public class Dept {
private int deptId;
private String deptName;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}</span></span>
映射文件:
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="my.test.entity">
<class name="Dept" table="t_dept">
<id name="deptId">
<generator class="native"></generator>
</id>
<property name="deptName"></property>
</class>
</hibernate-mapping></span>
2、dao层代码如下:
<span style="font-size:14px;"><span style="white-space:pre"> </span>public class DeptDao {
// IOC容器注入hibernateTemplate
private HibernateTemplate hibernateTemplate;
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
public void save(Dept dept) {
hibernateTemplate.save(dept);
}
public void delete(Dept dept) {
hibernateTemplate.delete(dept);
}
public void update(Dept dept) {
hibernateTemplate.update(dept);
}
}</span>
3、service层代码如下:
<span style="font-size:14px;"><span style="white-space:pre"> </span>public class DeptService {
private DeptDao deptDao;
public void setDeptDao(DeptDao deptDao) {
this.deptDao = deptDao;
}
public void save() {
deptDao.save(new Dept());
}
}</span>
4、action代码如下:
<span style="font-size:14px;"><span style="white-space:pre"> </span>public class DeptAction extends ActionSupport{
<span style="white-space:pre"> </span>// IOC容器注入此属性
<span style="white-space:pre"> </span>private DeptService deptService;
<span style="white-space:pre"> </span>public void setDeptService(DeptService deptService) {
<span style="white-space:pre"> </span>this.deptService = deptService;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String save() throws Exception {
<span style="white-space:pre"> </span>deptService.save(new Dept());
<span style="white-space:pre"> </span>return SUCCESS;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String get() throws Exception {
<span style="white-space:pre"> </span>return SUCCESS;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}</span>
5、Struts.xml
<span style="font-size:14px;"><span style="white-space:pre"> <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
<span style="white-space:pre"> </span>"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
<span style="white-space:pre"> </span>"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<span style="white-space:pre"> </span><package name="ssh" extends="struts-default">
<span style="white-space:pre"> </span><action name="dept_*" class="deptAction" method="{1}">
<span style="white-space:pre"> </span><result name="success">/index.jsp</result>
<span style="white-space:pre"> </span></action>
<span style="white-space:pre"> </span></package>
</struts></span></span>
6、applicationContext.xml
<span style="font-size:14px;"><span style="white-space:pre"> <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre"> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span style="white-space:pre"> </span>xmlns:p="http://www.springframework.org/schema/p"
<span style="white-space:pre"> </span>xmlns:context="http://www.springframework.org/schema/context"
<span style="white-space:pre"> </span>xmlns:aop="http://www.springframework.org/schema/aop"
<span style="white-space:pre"> </span>xmlns:tx="http://www.springframework.org/schema/tx"
<span style="white-space:pre"> </span>xsi:schemaLocation="
<span style="white-space:pre"> </span> http://www.springframework.org/schema/beans
<span style="white-space:pre"> </span> http://www.springframework.org/schema/beans/spring-beans.xsd
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span> 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
<span style="white-space:pre"> </span> http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<span style="white-space:pre"> </span><!-- 1. 创建Dao实例 , 注入SessionFactory属性-->
<span style="white-space:pre"> </span><bean id="deptDao" class="</span>my.test<span style="white-space:pre">.dao.DeptDao">
<span style="white-space:pre"> </span><property name="sessionFactory" ref="sessionFactory"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- 2. 创建Service实例 , 注入Dao属性-->
<span style="white-space:pre"> </span><bean id="deptService" class="</span><span style="font-family: Arial, Helvetica, sans-serif;">my.test</span><span style="font-family: Arial, Helvetica, sans-serif;">.service.DeptService"></span><span style="white-space:pre">
<span style="white-space:pre"> </span><property name="deptDao" ref="deptDao"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- 创建Action实例 -->
<span style="white-space:pre"> </span><bean id="deptAction" class="</span>my.test<span style="white-space:pre">.action.DeptAction">
<span style="white-space:pre"> </span><property name="deptService" ref="deptService"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- 3. 配置数据源 -->
<span style="white-space:pre"> </span><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<span style="white-space:pre"> </span><property name="jdbcUrl" value="jdbc:mysql:///</span>mytest<span style="white-space:pre">_mysql"></property>
<span style="white-space:pre"> </span><property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<span style="white-space:pre"> </span><property name="user" value="root"></property>
<span style="white-space:pre"> </span><property name="password" value="</span>root<span style="white-space:pre">"></property>
<span style="white-space:pre"> </span><property name="initialPoolSize" value="3"></property>
<span style="white-space:pre"> </span><property name="maxPoolSize" value="6"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- 4. 创建sessionFactory对象 -->
<span style="white-space:pre"> </span><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<span style="white-space:pre"> </span><property name="dataSource" ref="dataSource"></property>
<span style="white-space:pre"> </span><property name="hibernateProperties">
<span style="white-space:pre"> </span><props>
<span style="white-space:pre"> </span><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<span style="white-space:pre"> </span><prop key="hibernate.show_sql">true</prop>
<span style="white-space:pre"> </span><prop key="hibernate.hbm2ddl.auto">update</prop>
<span style="white-space:pre"> </span></props>
<span style="white-space:pre"> </span></property>
<span style="white-space:pre"> </span><property name="mappingLocations">
<span style="white-space:pre"> </span><list>
<span style="white-space:pre"> </span><value>classpath:cn/itcast/entity/*.hbm.xml</value>
<span style="white-space:pre"> </span></list>
<span style="white-space:pre"> </span></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span><!-- *****************5. Spring声明式事务管理***************** -->
<span style="white-space:pre"> </span><!-- 5.1 配置事务管理器 -->
<span style="white-space:pre"> </span><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<span style="white-space:pre"> </span><property name="sessionFactory" ref="sessionFactory"></property>
<span style="white-space:pre"> </span></bean>
<span style="white-space:pre"> </span><!-- 5.2 配置事务增强 (进行怎样的事务管理) -->
<span style="white-space:pre"> </span><tx:advice id="txAdvice" transaction-manager="txManager">
<span style="white-space:pre"> </span><tx:attributes>
<span style="white-space:pre"> </span><tx:method name="*" read-only="false"/>
<span style="white-space:pre"> </span></tx:attributes>
<span style="white-space:pre"> </span></tx:advice>
<span style="white-space:pre"> </span><!-- 5.3配置切面 = 增强 + 切入点 -->
<span style="white-space:pre"> </span><aop:config>
<span style="white-space:pre"> </span><aop:pointcut expression="execution(* </span>my.test<span style="white-space:pre">.service.*Service.*(..))" id="pt"/>
<span style="white-space:pre"> </span><aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
<span style="white-space:pre"> </span></aop:config>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
</beans> </span></span>