白猿传:SSH整合(struts2.2.1 + spring3.0 + hibernate3.3)

来源:百度文库 编辑:中财网 时间:2024/04/24 06:20:51
使用工具MyEclipse8.6

过程:
版本:struts2.2.1 + spring3.0 + hibernate3.3
MyEclipse8.6引入JAR包

1.引入JAR包:
a)可以使用myeclipse自带的功能引入所需要的包:
  右键工程-->MyEclipse--> add Hibernate capabilities,add spring capabilities

b)struts2的包可以从 下载的目录下复制有 七 个包
  例如:E:/CL/API/struts-2.2.1.1-all/struts-2.2.1.1/apps/struts2-blank/WEB-INF/lib

c)还需要插件包 struts2-spring-plugin-2.2.1.1.jar
  例如:E:/CL/API/struts-2.2.1.1-all/struts-2.2.1.1/lib



2.配置文件:(主要是web.xml和applicationContext.xml的配置)

web.xml:

   
        struts2
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   


   
        struts2
        /*
   


   
        contextConfigLocation
        classpath:applicationContext.xml
   


   
        org.springframework.web.context.ContextLoaderListener
   



applicationContext.xml:

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

   
   

   
   

            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       
       

   


   
       
           
       

   




hibernate.cfg.xml:

              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

   

       
           
                org.hibernate.dialect.Oracle9Dialect
           

           
                jdbc:oracle:thin:@localhost:1521:oracle
           

            chenl
            chenl
           
                oracle.jdbc.driver.OracleDriver
           


           
       


   



struts.xml:
            "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

   
       
       
       
           
           

       

   


图片步骤:

引入spring的包

 

引入hibernate的包:

一直下一步。。。


结束.

PS:

使用hibernateTemplate:

Action中代码:

@Component("userAction")
@Scope("prototype")
public class UserAction {

@Resource
private HibernateTemplate hibernateTemplate;

public void execute() {
  TUser user = new TUser();
  user.setUsername("aaa");

  TDetail tdetail = new TDetail();
  tdetail.setIdcard("11111111111");
  tdetail.setEmail("aa@qq.com");

  tdetail.setTuser(user);
  user.setTdetail(tdetail);

  hibernateTemplate.save(user);
  hibernateTemplate.flush();
}
}
注意: 由于是比较新的版本 所以 整合后需要用到asm3.3版本的jar包而不是asm.jar