目前对于这三大框架整合开发有很多人有这不同的看法,我在这提供一个自己比较喜欢的设计方式:
下面以搭建一个用户管理实验做为说明:
第一步:创建数据库(只需要建立数据库名字就可以了)
第二步:在myeclipse中切换到myeclipse database explorer 然后建立数据库连接(datebase driver)如图:我这里取名为:usermanager   完成待用
 
struts2+hibernate3.3+spring3.0  实现零配置_struts2
第三步:切换回myeclipse java enterprise界面;建立自己的项目:如我这里叫usermanager
第四步:建立对应的包:分别有action  service  serviceimpl  dao  daoimpl pojo  vo   tools用mvc设计模式    分三层一般建立这几个包就够了。
第五步:建立实体类并写好注解:如我这里就建立了:
package com.cn.nnny.ssh.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
//用户实体
@Entity
@Table(name="users")
public class Users {
 private int  id;// 编号
 private String  name;//姓名
 private int age;//年龄
 private String  sex;//性别    男  女
 private Department department;//部门
 private String  telphone;//电话
 
 
 public Users(){
  
 }
@Id
@GeneratedValue
 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public String getSex() {
  return sex;
 }

 public void setSex(String sex) {
  this.sex = sex;
 }
@ManyToOne
 public Department getDepartment() {
  return department;
 }

 public void setDepartment(Department department) {
  this.department = department;
 }

 public String getTelphone() {
  return telphone;
 }

 public void setTelphone(String telphone) {
  this.telphone = telphone;
 }
 
 
  
}
*********************************************************8
package com.cn.nnny.ssh.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
//部门实体
@Entity
public class Department {
    private int depid;//编号
    private String depname;//名称
    private String address;//地址
   
    public Department(){
     
    }
@Id
@GeneratedValue
 public int getDepid() {
  return depid;
 }
 public void setDepid(int depid) {
  this.depid = depid;
 }
 public String getDepname() {
  return depname;
 }
 public void setDepname(String depname) {
  this.depname = depname;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
}
************************************************
第六步:加入三大框架的jar包:注意使用myeclipse给你添加包:特别是添加hibernate的支持的时候要选中之前创建的usermanager驱动(就是前面创建备用的那个)
有这几个基本够用:struts2+hibernate3.3+spring3.0  实现零配置_spring3.0_02
 
第七步:完成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:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-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/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 <context:annotation-config /><!-- 注解 -->
 <context:component-scan base-package="com.cn.nnny.ssh*.*" /><!-- 扫描包 -->
 <aop:aspectj-autoproxy /><!-- 代理 -->
 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 
  <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
  </property>
  <property name="url"
   value="jdbc:sqlserver://localhost:1433;databaseName=hibernate">
  </property>
  <property name="username" value="sa"></property>
  <property name="password" value="sa"></property>
 </bean>
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan">   
      <list>
        <value>com.cn.nnny.usermanager.pojo</value> 
      
      </list>
    </property>
     <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
   </props>
  </property>
  </bean> 
 
 
<!-- 定义事务管理器 -->
 <bean id="txManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <!--<tx:annotation-driven transaction-manager="txManager"/> 申明annotation 加载事务驱动 -->
 
 
 <tx:advice id="txAdvice" transaction-manager="txManager">
  <tx:attributes>
   <tx:method name="list*" read-only="true" /><!-- 加事务的方法 -->
   <tx:method name="save*" propagation="REQUIRED"/><!-- 事务的生成特性 -->
   <tx:method name="delete*" propagation="REQUIRED"/><!-- 事务的生成特性 -->
   <tx:method name="update*" propagation="REQUIRED"/><!-- 事务的生成特性 -->
  </tx:attributes>
 </tx:advice>
 <aop:config>
  <aop:pointcut id="bussinessService" expression="execution(public * com.cn.nnny.ssh.service..*.*(..))" />
  <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
 </aop:config>

</beans>
****************************************************
第八步:部署项目到应用服务器上(我用tomcat)然后启动这样它就会给你自己创建数据库表以及他们之间的关系了.
*********************web。xml文件配置***********************888888
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 
 
  <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>*.action</url-pattern>
  </filter-mapping></web-app>
****************************************************
 
第九步:方向工程(注意我们这里需要把生成的dao放到pojo这个包下  选择dao是选spring那个   至于为什么试试就知道了)对于不会反向工程的自己查查很简单的附个图:
struts2+hibernate3.3+spring3.0  实现零配置_hibernate3.3_03
 
 
对了:有的时候myeclipse不给你选择struts2+hibernate3.3+spring3.0  实现零配置_struts2_04
那么你需要修改spring配置文件中
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      
改成:
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
改后下面会报错 删除就可以了  等反向工程做完在改回来就行了
 
然后完成反向工程:
 
最后一步:
回到myeclipse中提取接口:然后把相应的类移到相应的包中就ok
(不要说不会用myeclipse自动提取接口哦!!)
************************************************************
最后附上action、service 、dao层的注解:
哦忘了;在dao实现类中腰加这么一句:
 
 @Autowired
 public void setSessionFactoryOverride(SessionFactory sessionFactory) {
  super.setSessionFactory(sessionFactory);
 }
***********************action中**********************************
package com.cn.nnny.ssh.action;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.cn.nnny.ssh.pojo.Department;
import com.cn.nnny.ssh.service.IDepartmentService;
import com.cn.nnny.ssh.vo.DepartmentVo;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope(value = "prototype")
@Namespace(value = "/department")
public class DepartmenAction extends ActionSupport {
 
 @Autowired
 private IDepartmentService service;//拿到服务
 @Autowired
 private DepartmentVo departmentVo;
 
 private List<Department> departmentlist;
 
   
 @Action(value = "index", results = {
   @Result(name = "success", location = "/WEB-INF/jsp/department/index.jsp"),
   @Result(name = "errors", location = "/WEB-INF/jsp/department/index.jsp") })
 public String execute() throws Exception {
  return super.execute();
 }
 
 
 @Action(value = "save", results = {
   @Result(name = "success", location = "/WEB-INF/jsp/department/index.jsp"),
   @Result(name = "errors", location = "/WEB-INF/jsp/department/index.jsp") })
 public String save() throws Exception {
  System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwww");
  service.save(departmentVo);
  return super.execute();
 }
 
 
 @Action(value = "list", results = {
   @Result(name = "success", location = "/WEB-INF/jsp/department/index.jsp"),
   @Result(name = "errors", location = "/WEB-INF/jsp/department/index.jsp") })
 public String list() throws Exception {
  
  departmentlist=service.list();
  return super.execute();
 }
 
 

 public DepartmentVo getDepartmentVo() {//必须有set方法  才能收集值
  return departmentVo;
 }
 public void setDepartmentVo(DepartmentVo departmentVo) {
  this.departmentVo = departmentVo;
 }

 public List<Department> getDepartmentlist() {
  return departmentlist;
 }

 public void setDepartmentlist(List<Department> departmentlist) {
  this.departmentlist = departmentlist;
 }

 
 
}
****************dao中**************只附关键的*********88
@Repository
public class DepartmentDAO extends HibernateDaoSupport implements IDepartmentDAO {
 
 private static final Logger log = LoggerFactory.getLogger(DepartmentDAO.class);
 protected void initDao() {
  // do nothing
 }
 /* (non-Javadoc)
  * @see com.cn.nnny.ssh.dao.IDepartmentDAO#save(com.cn.nnny.ssh.pojo.Department)
  */
 @Autowired
 public void setSessionFactoryOverride(SessionFactory sessionFactory) {
  super.setSessionFactory(sessionFactory);
 }
 
 public void save(Department transientInstance) {
  log.debug("saving Department instance");
  try {
   getHibernateTemplate().save(transientInstance);
   log.debug("save successful");
  } catch (RuntimeException re) {
   log.error("save failed", re);
   throw re;
  }
 }
*********************service中************************************
package com.cn.nnny.ssh.service.impl;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cn.nnny.ssh.dao.IDepartmentDAO;
import com.cn.nnny.ssh.pojo.Department;
import com.cn.nnny.ssh.service.IDepartmentService;
import com.cn.nnny.ssh.vo.DepartmentVo;
import com.sun.org.apache.commons.beanutils.BeanUtils;
@Service
public class DepartmentService implements IDepartmentService {
 
 @Autowired
 private IDepartmentDAO departmentDAO;
 
 public boolean save(DepartmentVo departmentVo){
  boolean ok=true;
  Department department=new Department();
  try {
   BeanUtils.copyProperties(department, departmentVo);//值复制对应名字的
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  departmentDAO.save(department);
  System.out.println(departmentVo.getDepname());
  return ok;
  
 }
 
 public List<Department> list(){
  
  List<Department> departmentlist=departmentDAO.findAll();
  
    return departmentlist;
  
 }
}
************************ok结束**********************************