第一步:定制 service接口,为什么用接口我也不清楚
package com.inspur.services;
import com.hsp.domain.User;
public interface IaddService {
public void addUser(User e);
}
第二步,实现service接口
package com.inspur.services;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;
import com.hsp.domain.User;
//要是没有这个transactional将会导致如下错误:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
@Transactional
public class AddUser implements IaddService {
//注 这里的sf必须与在applicationContext.xml中的属性sf一致
private SessionFactory sf;
public SessionFactory getSf() {
return sf;
}
public void setSf(SessionFactory sf) {
this.sf = sf;
}
public void addUser(User e) {
sf.getCurrentSession().save(e);
}
}
第三步:在spring容器中部署(applicationContext.xml)
<!-- 配置EmployeeService对象 -->
<bean id="userSevice" class="com.inspur.services.AddUser">
<property name="sf" ref="sessionFactory" />
</bean>
第四步:在action中使用
package com.inspur.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.hsp.domain.User;
import com.inspur.forms.*;
import com.inspur.services.AddUser;
import com.inspur.services.IaddService;
public class LoginAction extends DispatchAction {
//注这里的userservice必须与applicationContext.xml中的配置的loginaction的属性userservice属性名字一致
private IaddService userservice;
public IaddService getUserservice() {
return userservice;
}
public void setUserservice(IaddService userservice) {
this.userservice = userservice;
}
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("******通过新的方式响应请求***********");
/*
EmployeeForm employeeForm=(EmployeeForm)form;
//构建一个Employee对象
Employee e=new Employee();
e.setId(employeeForm.getId());
e.setName(employeeForm.getName());
e.setLeader(employeeForm.getLeader());
e.setMid("111111");
eif.addEmployee(e);
request.getSession().setAttribute("adder", e);
*/
User u=new User();
u.setId(1);
u.setName("wj");
u.setPassword("123");
userservice.addUser(u);
return mapping.findForward("add");
}
//响应登录
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
System.out.println("******通过新的方式响应请求***********");
/*
EmployeeForm employeeForm=(EmployeeForm)form;
//构建一个Employee对象
Employee e=new Employee();
e.setId(employeeForm.getId());
e.setName(employeeForm.getName());
e.setLeader(employeeForm.getLeader());
e.setMid("111111");
eif.addEmployee(e);
request.getSession().setAttribute("adder", e);
*/
return mapping.findForward("ok");
}
}
郑重补充:
要想使用重量级工具最好用单例模式,可以通过spring的依赖注入实现这个问题,但是本例中LoginACtion以及AddUser使用了依赖注入后的对象sessionfactory,userservice,因此在类中必须做到变量名字与applicationContext.xml的一致,且用上一级的接口或者类来接受依赖注入后的对象。
一般都会需要在applicationContext.xml中配置代理事物管理:
<!-- 启用注解扫描 -->
<context:annotation-config/>
<!-- 配置事务管理器,统一管理sessionFactory的事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 启用事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
然后在AddUser中添加@Transactional,由于使用了注解扫描,所以web容器会识别出AddUser使用了事物管理器以及事物注解。