ssh最新整合配置(struts2.2.1+ hibernate3.6+spring3.0.5)

今天给大家介绍一下最新版本的SSH(struts2.2.1+ hibernate3.6+spring3.0.5)组合。注意本讲解为手工搭建! 

一、为SSH做好准备 

struts2-2.2.1-all.zip       
hibernate-distribution-3.6.0.Final-dist.zip 
spring-framework-3.0.5.RELEASE.zip 
spring-framework-2.5.6-with-dependencies.zip 
slf4j-1.6.1.zip 
apache-tomcat-6.0.29.zip 
mysql-connector-java-5.1.13-bin.jar 
mysql-essential-5.1.53-win32.msi

工具用eclipse或者myeclipse 文件都行

 

二、搭建开发环境 打开MyEclipse,新建一个web project (选择Java EE5.0)

 

三、需要的jar包

        1、hibernate-3.6.0 配置

             

Java代码    收藏代码
  1. hibernate-distribution-3.6.0.Final-dist.zip中需要如下jar  
  2.   
  3. hibernate3.jar   
  4. lib/required/antlr-2.7.6.jar   
  5. lib/required/commons-collections-3.1.jar   
  6. lib/required/dom4j-1.6.1.jar   
  7. lib/required/javassist-3.12.0.GA.jar   
  8. lib/required/jta-1.1.jar   
  9. lib/required/slf4j-api-1.6.1.jar   
  10. lib/jpa/hibernate-jpa-2.0-api-1.0.0.Final.jar //新版本需要jar  
  11.   
  12. slf4j-1.6.1.zip中需要如下jar  
  13.   
  14. slf4j-nop-1.6.1.jar  
  15.   
  16. mysql-connector-java-5.1.13-bin.jar //mysql 的驱动包  

   注意:新版本已经和Annotation做了组合 要用Annotation不需要另外加入jar。

    在测试的时候也不需要第一种写法:

Java代码    收藏代码
  1. SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory(); //现在已经过时  

    用第二种:

Java代码    收藏代码
  1. SessionFactory  sf = new Configuration().configure().buildSessionFactory();//可以直接使用Annotation  

 

   

 

 

   2、spring 3.0.5配置

        

Java代码    收藏代码
  1. spring-framework-3.0.5.RELEASE.zip中需要如下jar  
  2.   
  3. dist/* //为了方便考入此目录下的所有jar,不想全部考入的自己选择  
  4.   
  5. spring-framework-2.5.6-with-dependencies.zip 在此包中考入spring  
  6.   
  7. aopalliance/aopalliance.jar   
  8. aspectj/aspectjrt.jar   
  9. aspectj/aspectjweaver.jar   
  10. cglib/cglib-nodep-2.1_3.jar   
  11. jakarta-commons/commons-pool.jar   
  12. jakarta-commons/commons-dbcp.jar   
  13. jakarta-commons/commons-logging.jar   

    大家可以看到有了spring2.5.6的包 3.0所需要的其他类就能在其中找比较方便。

    注意:cglib-nodep-2.1_3.jar   包也可以换成asm-2.2.3.jar和cglib-2.2.jar



 

 3、struts2.2.1 配置

 

Java代码    收藏代码
  1. struts2-2.2.1-all.zip 中加入如下jar     
  2.   
  3. lib/ognl-3.0.jar    
  4. lib/xwork-core-2.2.1.jar   
  5. lib/freemarker-2.3.16.jar   
  6. lib/struts2-core-2.2.1.jar   
  7. lib/struts2-spring-plugin-2.2.1.jar   
  8. lib/commons-io-1.3.2.jar   
  9. lib/commons-fileupload-1.2.1.jar   
  10. lib/commons-logging-1.0.4.jar  
  11.   
  12. javassist-3.7.ga.jar //这个包在lib下没有;从apps/struts2-blank-2.2.1.war中的lib文件里找到  

   注意:如果使用ognl的jar包是2.7以下的就不用 javassist-3.7.ga.jar 了

 

     到此为止所有的jar包就加完毕了 javassist-3.7.ga.jar 和 commons-logging.jar 已经重复删除不需要的(保留版本高的就行)。总共是44个jar

 



  

 

四、XML文件配置 

Applicationcontext.xml代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.            http://www.springframework.org/schema/context  
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.            http://www.springframework.org/schema/aop  
  10.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  11.            http://www.springframework.org/schema/tx   
  12.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  13.     <!-- 用注解方式注入bean -->  
  14.     <context:annotation-config/>  
  15.     <context:component-scan base-package="com.yj"/>  
  16.     <!-- 数据库连接池 -->  
  17.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  18.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  19.         <property name="url" value="jdbc:mysql://localhost:3306/sshtest" />  
  20.         <property name="username" value="root" />  
  21.         <property name="password" value="root" />  
  22.     </bean>  
  23.     <!-- hibernate sessionFactory 创建 -->  
  24.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  25.         <property name="dataSource" ref="dataSource"/>  
  26.         <property name="packagesToScan">  
  27.             <list>  
  28.                 <value>com.yj.model</value>  
  29.             </list>  
  30.         </property>  
  31.         <property name="hibernateProperties">  
  32.             <props>  
  33.                 <prop key="hibernate.format_sql">true</prop>  
  34.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  35.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  36.             </props>  
  37.         </property>  
  38.     </bean>  
  39.      <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">  
  40.         <property name="sessionFactory" ref="sessionFactory"></property>  
  41.      </bean>       
  42.      <!-- 事物配置 -->  
  43.      <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  44.         <property name="sessionFactory" ref="sessionFactory"></property>  
  45.      </bean>  
  46.      <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  47.         <tx:attributes>  
  48.             <tx:method name="find*" read-only="true"/>  
  49.             <tx:method name="add*" propagation="REQUIRED"/>  
  50.         </tx:attributes>  
  51.      </tx:advice>  
  52.      <aop:config>  
  53.         <aop:pointcut expression="execution(public * com.yj.service..*.*(..))" id="myPointcut"/>  
  54.         <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>  
  55.      </aop:config>  
  56.        
  57. </beans>  

 

 

 

Web.xml代码    收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <welcome-file-list>  
  7.         <welcome-file>index.jsp</welcome-file>  
  8.     </welcome-file-list>  
  9.     <context-param>  
  10.         <param-name>contextConfigLocation</param-name>  
  11.         <param-value>classpath:applicationContext.xml</param-value>  
  12.     </context-param>  
  13.     <listener>  
  14.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  15.     </listener>  
  16.     <filter>  
  17.         <filter-name>openSessionInViewFilter</filter-name>  
  18.         <filter-class>  
  19.             org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  20.     </filter>  
  21.     <filter-mapping>  
  22.         <filter-name>openSessionInViewFilter</filter-name>  
  23.         <url-pattern>/*</url-pattern>  
  24.     </filter-mapping>  
  25.     <filter>  
  26.         <filter-name>struts2</filter-name>  
  27.         <filter-class>  
  28.             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  29.     </filter>  
  30.     <filter-mapping>  
  31.         <filter-name>struts2</filter-name>  
  32.         <url-pattern>/*</url-pattern>  
  33.     </filter-mapping>  
  34. </web-app>  

 

   注意:1、只用在spring中配置了事物才能在web.xml配置openSessionInViewFilter

             否则会出错。

             2、openSessionInViewFilter必须配置在strutsfilter之前。

 

结束:我把自己的demo上传到附件 没有jar文件需要的自己下载把,也可以联系我。关于demo中的问题是service和demo都没有提取接口这样在开发中是不允许的。各位不要学我

 

在demo中我所有的测试spring 的测试 需要junit-4.4以上版本。用这个测试的好处是测试不会破坏数据库的内容,因为是事物级的测试。

 

Java代码    收藏代码
  1. package com.yj.service;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.test.context.ContextConfiguration;  
  6. import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;  
  7.   
  8. import com.yj.model.User;  
  9.   
  10. @ContextConfiguration("classpath:applicationContext.xml")  
  11. public class UserServiceTest extends AbstractTransactionalJUnit4SpringContextTests {  
  12.       
  13.     @Autowired  
  14.     private UserService userService;  
  15.   
  16.     @Test  
  17.     public void testSave() {  
  18.         User user = new User();  
  19.         user.setPassword("1234");  
  20.         user.setUsername("张三3");  
  21.         userService.save(user);  
  22.     }  
  23.   
  24.     @Test  
  25.     public void testExits() {  
  26.         UserService userService = new UserService();  
  27.         User user = new User();  
  28.         user.setPassword("1234");  
  29.         user.setUsername("张三");  
  30.         userService.exits(user.getUsername());  
  31.           
  32.     }  
  33.   
  34. }
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值