
1 package com.ssh.dao;
2
3 public interface IBaseDao {
4
5 }
1 package com.ssh.dao;
2
3 public interface IUserDao extends IBaseDao {
4
5 }
1 package com.ssh.dao.impl;
2
3 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
4 import com.ssh.dao.IBaseDao;
5
6 public class BaseDaoHibernateImpl extends HibernateDaoSupport implements IBaseDao {
7
8 }
1 package com.ssh.dao.impl;
2
3 import com.ssh.dao.IUserDao;
4
5 public class UserDaoHibernateImpl extends BaseDaoHibernateImpl implements IUserDao {
6
7 }
1 package com.ssh.service;
2
3 public interface IUserService {
4
5 }
1 package com.ssh.service.impl;
2
3 import com.ssh.dao.IUserDao;
4 import com.ssh.service.IUserService;
5
6 public class UserServiceImpl implements IUserService {
7 private IUserDao userDaoImpl ;
8
9 public IUserDao getUserDaoImpl() {
10 return userDaoImpl;
11 }
12
13 public void setUserDaoImpl(IUserDao userDaoImpl) {
14 this.userDaoImpl = userDaoImpl;
15 }
16 }
1 package com.ssh.web.action;
2
3 import org.apache.struts.actions.DispatchAction;
4
5 public class BaseAction extends DispatchAction {
6
7 }
1 package com.ssh.web.action;
2
3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse;
5
6 import org.apache.struts.action.ActionForm;
7 import org.apache.struts.action.ActionForward;
8 import org.apache.struts.action.ActionMapping;
9
10 import com.ssh.service.IUserService;
11
12 public class SearchAction extends BaseAction {
13 private IUserService userServiceImpl ;
14
15 public IUserService getUserServiceImpl() {
16 return userServiceImpl;
17 }
18
19 public void setUserServiceImpl(IUserService userServiceImpl) {
20 this.userServiceImpl = userServiceImpl;
21 }
22
23 public ActionForward search(ActionMapping mapping, ActionForm form,
24 HttpServletRequest request, HttpServletResponse response)
25 throws Exception {
26
27 return super.execute(mapping, form, request, response);
28 }
29
30 }
1 package com.ssh.web.action.form;
2
3 import org.apache.struts.action.ActionForm;
4
5 public class SearchActionForm extends ActionForm {
6
7 }
四份配置文件之一:applicationContext-common.xml
配置数据源、接管SessionFactory Bean、将事务横切到 service层 , 设置事务规则。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:aop="http://www.springframework.org/schema/aop"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
10 >
11
12 <bean id="dataSource"
13 class="org.apache.commons.dbcp.BasicDataSource">
14 <property name="driverClassName"
15 value="com.mysql.jdbc.Driver">
16 </property>
17 <property name="url"
18 value="jdbc:mysql://localhost:3306/hibernate3x">
19 </property>
20 <property name="username" value="root"></property>
21 <property name="password" value="123456"></property>
22 </bean>
23 <bean id="sessionFactory"
24 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
25 <property name="dataSource">
26 <ref bean="dataSource" />
27 </property>
28 <property name="hibernateProperties">
29 <props>
30 <prop key="hibernate.dialect">
31 org.hibernate.dialect.MySQLDialect
32 </prop>
33 </props>
34 </property>
35 <property name="mappingResources">
36 <list>
37 <value>com/ssh/domain/Classes.hbm.xml</value>
38 <value>com/ssh/domain/Student.hbm.xml</value>
39 <value>com/ssh/domain/Teacher.hbm.xml</value></list>
40 </property>
41 </bean>
42
43 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
44 <property name="sessionFactory">
45 <ref bean="sessionFactory" />
46 </property>
47 </bean>
48
49 <tx:advice id="txAdvice" transaction-manager="transactionManager">
50 <tx:attributes>
51 <tx:method name="append*" propagation="REQUIRED"/>
52 <tx:method name="remove*" propagation="REQUIRED"/>
53 <tx:method name="modify*" propagation="REQUIRED"/>
54 <tx:method name="*" read-only="true"/>
55 </tx:attributes>
56 </tx:advice>
57
58 <aop:config>
59 <aop:pointcut id="all" expression="execution(* com.ssh.service.impl.*.*(..))"/>
60 <aop:advisor advice-ref="txAdvice" pointcut-ref="all"/>
61 </aop:config>
62 </beans>
四份配置文件之二:applicationContext-dao.xml
将sessionFactort注入Dao层 , 因为
UserDaoHibernateImpl extends BaseDaoHibernateImpl
BaseDaoHibernateImpl extends org.springframework.orm.hibernate3.support.HibernateDaoSupport
所以Dao层不需要写 sessionFactory的get/set访问器
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
6
7 <bean id="userDaoRef" class="com.lmm.ssh.dao.impl.UserDaoHibernateImpl">
8 <property name="sessionFactory" ref="sessionFactory"></property>
9 </bean>
10 </beans>
四份配置文件之三:applicationContext-service.xml
Service 层 需提供 userDaoImpl 的get/set访问器
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
6
7 <bean id="userServiceRef" class="com.lmm.ssh.service.impl.UserServiceImpl">
8 <property name="userDaoImpl" ref="userDaoRef"></property>
9 </bean>
10
11 </beans>
四份配置文件之四:applicationContext-action.xml
action 类 需提供 userServiceImpl get/set访问器
注意:此处 <bean name="/search" ...>
/search 为 Strtus-config.xml 中配置的 action 请求路径
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
6
7 <bean name="/search" class="com.lmm.ssh.web.action.SearchAction">
8 <property name="userServiceImpl" ref="userServiceRef"></property>
9 </bean>
10 </beans>
Strtus-config.xml 文件
整合Struts1 和 Spring 的关键在type中不写自己的Action类路径
采用Spring提供的代理类:org.springframework.web.struts.DelegatingActionProxy
注意:path="/search" 需要与 applicationContext-action.xml 中<bean name="/search" ...> 对应
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
3
4 <struts-config>
5 <data-sources />
6 <form-beans >
7 <form-bean name="searchActionForm" type="com.ssh.web.form.SearchActionForm" />
8 </form-beans>
9 <global-exceptions />
10 <global-forwards />
11 <action-mappings >
12 <action
13 input="/index.jsp"
14 name="searchActionForm"
15 parameter="method"
16 path="/search"
17 scope="request"
18 type="org.springframework.web.struts.DelegatingActionProxy">
19 <forward name="success" path="/index.jsp"></forward>
20 </action>
21 </action-mappings>
22 <message-resources parameter="com.ssh.web.ApplicationResources" />
23 </struts-config>
到此,Struts1.3 Spring2.5 Hibernate3.1 整合完成。