---
title: "struts2.1.6,spring2.5.6,hibernate3.3.1,annotations"
author: "hypgr"
date: "2009-04-24T15:50:47Z"
tags: []
slug: "struts2-1-6-spring2-5-6-hibernate3-3-1-annotations"
draft: false
lastmod: "2009-04-24T15:53:30Z"
id: 374061
url: "https://hypgr.iteye.com/blog/374061"
---

根据流程进行注意点的陈述  
1、测试页面输入用户名、密码、性别然后提交到usersActon  
[](/admin/blogs/attachments/200903/2984940890.jpg)![](http://tmsoft.lsxy.com/attachments/200903/2984940890_f2s.jpg)  
2、usersAction我并没有写入到struts.xml，因为我的整合中去掉了struts.xml，而是使用struts 2的struts2-convention-plugin-2.1.6.jar插件  
[](/admin/blogs/attachments/200903/9837211332.jpg)![](http://tmsoft.lsxy.com/attachments/200903/9837211332_f2s.jpg)  
在com.lsxy.tmsoft.action.UsersAction的execute() 方法上进行注解如下  
@Action(value = "usersAction", results = { @Result(name = "success", location = "/success.jsp") })  
 public String execute() throws Exception {  
  usersDao.save(user);  
  return "success";  
 }  
期中的value = "usersAction"中的usersAction, 就是1、要找的usersAction  
3、在com.lsxy.tmsoft.action.UsersAction中的usersDao利用注解根据类型（UsersDao）进行自动装配，指向com.lsxy.tmsoft.dao如下  
@Autowired  
 private UsersDao usersDao;  
4、在com.lsxy.tmsoft.dao.UsersDao中利用@Repository进行注解，spring能够根据注解自动装配  
package com.lsxy.tmsoft.dao;

import org.springframework.stereotype.Repository;

import com.lsxy.tmsoft.dao.support.HibernateDao;  
import com.lsxy.tmsoft.vo.Users;  
@Repository  
public class UsersDao extends HibernateDao&lt;Users, Integer&gt; {

}  
5、HibernateDao和SimpleHibernateDao完成对数据库的基本操作  
6、注意点：在SimpleHibernateDao中获得session不能用sessionFactory.getCurrentSession();而必须使用sessionFactory.openSession();，否则保存后不能保存数据到数据库  
7、其他关键的配置全部在applicationContext.xml中  
下面这句就是自动扫描com.lsxy.tmsoft.vo下的实体类，在实体类中完成实体和数据的映射  
&lt;property name="packagesToScan" value="com.lsxy.tmsoft.vo" /&gt;  
&lt;!-- 使Spring关注Annotation --&gt;  
 &lt;context:annotation-config /&gt;

 &lt;!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --&gt;  
 &lt;context:component-scan base-package="com.lsxy.tmsoft" /&gt;  
&lt;!-- 事务管理器配置,单数据源事务 --&gt;  
 &lt;bean id="transactionManager"  
  class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;  
  &lt;property name="sessionFactory" ref="sessionFactory" /&gt;  
 &lt;/bean&gt;

 &lt;!-- 使用annotation定义事务 --&gt;  
 &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;  
8、包结构  
[](/admin/blogs/attachments/200903/2547422790.jpg)![](http://tmsoft.lsxy.com/attachments/200903/2547422790_f2s.jpg)  
9、源代码下载  
[tmsoft.rar](http://tmsoft.lsxy.com/download.php?id=463) (15.04 MB )