spring,hibernate,spring框架整合

        SSH框架作为javaEE最经典的框架, 初学者整合这几个框架可能也是一件比较头痛的事情(包括我自己), 下面来进行框架的整合!

        一:   准备

            SSH框架介绍

           Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet, 处于MVC的控制层,Struts 2是Struts的下一代产品,个人认为: struts2~~struts+xwork; 

           Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。完成数据持久层的重任

          Spring是一个轻量级控制反转(IOC)和面向切面(AOP)的容器框架。IOC和AOP是spring的重要设计思想。

          整合: struts2和hibernate甚至spring都是可以单独使用的, 整合使得开发更加便利!整合中spring起着重要作用! 打个不是很准确的比喻: spring用二只手将struts和hibernate联系在一起!

       二:整合开始

           在整合中SSH的版本是非常重要的, 必须要关注版本! 这里版本为: struts2.3.2,  spring3, hibernate3.6

           (1)统一引入jar包

             jar下载地址:  http://download.csdn.net/detail/huangchongwen/9879857

           (2)配置web.xml文件

            

  <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>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

 

 

 

     (3) 整合开始

       

    struts2总配置文件:

  

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
      <!-- 常量配置 -->
        <!-- 禁用动态方法访问 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <!-- 配置成开发模式 -->
    <constant name="struts.devMode" value="false" />
    <!-- 配置拓展名为action -->
    <constant name="struts.action.extention" value="action" />
    <!-- 把主题配置成simple -->
    <constant name="struts.ui.theme" value="simple" />
    
    <!-- 导入配置文件 -->
    <include file="it/cast/test/config/test-struts.xml"></include>
    
</struts>
  
test-struts.xml配置文件,注意:这个可以根据你具体的需要引入到配置文件,这只是个例子!

  

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="test-struts" extends="struts-default">
         <action name="test_*" class="it.cast.test.action.TestAction" method="{1}">
             <result name="success">test/jsp/test.jsp</result>
         </action>
    </package>
</struts>

 

   

    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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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">
    
  
     
     <!-- 导入外部的properties配置文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!-- 配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
        <property name="driverClass" value="${driverClass}"></property>
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="${initialPoolSize}"></property>
        <!--连接池中保留的最小连接数。Default: 3 -->
        <property name="minPoolSize" value="3"></property>
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize" value="${maxPoolSize}"></property>
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement" value="3"></property>
        <!--最大空闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="1800"></property>
    </bean>
    
    <!--创建sessionFactory-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="javax.persistence.validation.mode">none</prop> </props> </property>

<property name="mappingLocations"> <list> <value>classpath:cn/itcast/nsfw/user/entity/*.hbm.xml</value> <value>classpath:it/cast/test/entity/*.hbm.xml</value> </list> </property> </bean> <!-- 事务管理开始--> <!--事务管理--> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 事务通知--> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="find*" read-only="true" /> <tx:method name="get*" read-only="true" /> <tx:method name="load*" read-only="true" /> <tx:method name="list*" read-only="true" /> <tx:method name="search*" read-only="true" /> <tx:method name="*" rollback-for="Throwable" /> </tx:attributes> </tx:advice> <!-- 配置需要进行事务控制的类 --> <aop:config> <aop:pointcut id="serviceOperation" expression="bean(*Service)" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" /> </aop:config> <!-- 事务管理结束 --> <!-- 所有业务dao的parent --> <bean id="baseDao" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!--导入配置文件--> <import resource="classpath:it/cast/test/config/*-applicationContext.xml"/> <import resource="classpath:cn/itcast/nsfw/*/config/*-spring.xml"/> </beans>

 


    (4)注意事项:
   web.xml, struts2和spring总配置文件必须引入, 导入局部配置文件。根据项目具体的需求修改代码!
   应该清楚框架整合部分的细节
   (5)自己写一个例子吧,  实践出真理啊!

        推荐文章:写的还行!

              http://blog.csdn.net/zhuanzhe117/article/details/48014545;

              

 

       

posted @ 2017-06-24 22:33  BlueWolfThe  阅读(182)  评论(0编辑  收藏  举报