1.导入jar包
2.建包
3.添加Hibernate spring struts的支持(配置文件)
4.利用反向工程生成实体类(ps:添加注解支持Hibernate3.2版本以上)。
5.在struts的配置文件中添加动态调用
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
6.在spring中的配置文件中修改头文件为
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
7.加载配置信息
<context:component-scan base-package="cn.*" />
8.添加sessionFactory节点,代码如下
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
9.将sessionFactory节点注入带Hibernate模板中代码如下
<bean id="HibernateTimplte" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
10.创建事务管理、
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
abstract="false" lazy-init="default" autowire="default"
dependency-check="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
11.声明事物管理
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
12.通过aop来进行事物管理
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* cn.biz.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
13.则编写各层的接口声明方法
14.在dao层的实现接口的方法其用Hibernate模板技术创建HibernateTemplate。为其注入spring配置的bean节点。
代码如下
@Repository("comdao")
public class CommonDaoImpl implements CommonDao {
@Resource(name="HibernateTimplte")
HibernateTemplate hibernatetem;
@Override
public void save(Object object) {
hibernatetem.save(object);
}
15在biz的实现类中的注入代码如下
@Service("empbiz")
public class EmpBizImpl implements EmpBiz {
@Resource(name="comdao")
CommonDao commondao;
16.在action中需要注入struts的信息与spring的信息代码
@Controller("empaction")
@Namespace("/")
@ParentPackage("struts-default")
@Scope(value="prototype")
public class EmpAction extends ActionSupport {
@Resource(name="empbiz")
EmpBiz empbiz;
public EmpBiz getEmpbiz() {
return empbiz;
}
public void setEmpbiz(EmpBiz empbiz) {
this.empbiz = empbiz;
}
@Action(value="emp-list",results={
@Result(name="success",location="/index.jsp",type="redirect"),
@Result(name="error",location="/error.jsp",type="redirect")
})
public String getemplist()throws Exception{
try {
System.out.println("^_^^_^^_^");
empbiz.getEmpList();
} catch (Exception e) {
throw new Exception(e);
}
return SUCCESS;
}
17.在地址栏中输入http://localhost:8080/SSHZJ/emp-list!getemplist则转入index.jsp页面。