SSH框架整合

框架整合原理

Spring 和 Struts2 整合

                    用spring容器管理action 对象,action对象交给spring容器负责创建

Spring 和 hibernate 整合

                   用spring容器管理 sessionfactory  、spring负责session维护以及aop事务

框架整合的步骤

  1. 导包(如果是maven项目就 找相关依赖)

                   core |beans |context |expression |logging |log4j  spring基本包 加上日志包

                   spring-web 包                      spring整合web

                  spring-aop|spring-aspect|aop联盟|aopweaving            spring 整合aop

                   spring-jdbc|spring-tx|c3p0|spring-orm                         spring整合hibernate 以及事务包

                        spring-test                                                              Junit测试包   

    2、在xml配置(单独配置spring

<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>

            确保项目启动就读取spring的配置文件,这样spring的对象就会全部被创建,只要启动不报错,则配置成功

   单独 配置(Struts2)

配置struts2核心过滤器到web.xml

    <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>/*</url-pattern>
    </filter-mapping>

 

spring整合struts2(2种整合方案)

还是由struts2自己创建对象,然后由spring管理service对象

在spring的核心配置文件,配置action对象 同时配置service对象

在Struts2的核心配置文件中action,     class 属性采用完整路径名

 

第二种

在struts2 的核心配置文件中,action class 属性采用spring中对象的属性名

并且开启spring框架的常量

 <constant name="struts.objectFactory" value="spring"></constant>

单独配置hibernate环境

 

<hibernate-configuration>
    <session-factory>
        <!-- 数据库驱动 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 数据库url -->
        <property name="hibernate.connection.url">jdbc:mysql:///ssh</property>
        <!-- 数据库连接用户名 -->
        <property name="hibernate.connection.username">root</property>
        <!-- 数据库连接密码 -->
        <property name="hibernate.connection.password">1234</property>


        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- 将hibernate生成的sql语句打印到控制台 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 将hibernate生成的sql语句格式化(语法缩进) -->
        <property name="hibernate.format_sql">true</property>


        <property name="hibernate.hbm2ddl.auto">update</property>
        <!--事务隔离级别
        1 脏读
        2 不可重复读
        4 幻读
        8 串行化
        -->
        <property name="connection.isolation">4</property>

        <!--事务线程绑定-->
        <!--<property name="hibernate.current_session_context_class">thread</property>-->

        <!--引入实体类配置文件-->
        <mapping resource="hibernateMap/User.hbm.xml"></mapping>

    </session-factory>
</hibernate-configuration>

Spring整合hibernate

  1. 两种方式

第一种:spring直接读取hibernate 核心配置文件

并且管理sessionfactory对象

<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<bean>

第二种:将hibernate配置的属性全部写入spring核心配置文件中

配置 c3p0 连接池

 <!-- 指定spring读取db.properties配置 -->
    <context:property-placeholder location="classpath:db.properties"  />

    <!--c3p0连接池-->
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--spring整合hibernate-->
    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.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.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        //读取hibernate映射文件
        <property name="mappingDirectoryLocations" value="classpath:hibernateMap"></property>
 </bean>

spring的aop事务

<!--配置事务管理器-->
    <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!--配置事务模板-->
    <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"></property>
    </bean>

    <!--事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
            <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
            <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
            <tx:method name="transfer*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
        </tx:attributes>
    </tx:advice>

    <!--aop的植入-->
    <aop:config>
        <aop:pointcut id="pt" expression="execution(* com.fdw.service.impl.*ServiceImpl.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
    </aop:config>

SSH 三大框架的整合基本配置大致就是这,希望对浏览的此贴的 人有用,如有不足之处请指出(感谢,只求共同进步)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值