spring&SSH整合

SSH整合

![SSH.png][1]

Struts2和Spring的整合方式:Action类由Spring创建

jar包:
struts2的jar包:
http://www.suyibk.top/usr/uploads/2018/04/2693830169.png
hibernate的jar包:
![SSH-hibernate的jar包.png][3]
spring的jar包:
![SSH-spring的jar包.png][4]
引入插件包
    struts2-spring-plugin-2.3.24.jar
Action交给Spring管理:
将Action配置到Spring中.
    <!-- 配置Action -->
    <bean id="customerAction" class="cn.itcast.ssh.web.action.CustomerAction" scope="prototype">
     <!—必须手动注入属性-->
        <property name="customerService" ref="customerService"/>
    </bean>
Action的配置:
    <package name="ssh" extends="struts-default" namespace="/">
        <action name="customer_*" class="customerAction(配置的是spring中的id)" method="{1}">
        
        </action>
    </package>
不带Hibernate的配置文件:

替换数据库连接参数和连接池的配置
创建jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssh1
jdbc.username=root
jdbc.password=123
在Spring中引入外部属性文件
    <!-- 引入外部属性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
配置连接池:
      <!-- 配置c3p0连接池: -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
配置Hibernate的其他属性及映射,使用Spring框架提供的jdbc模板类
<!-- 配置Hibernate中的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 注入连接池 -->
        <property name="dataSource" ref="dataSource"/>
        
        <!-- 配置Hibernate的相关属性 -->
        <property name="hibernateProperties">
            <props>
                <!-- 配置Hibernate的方言 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- 显示SQL -->
                <prop key="hibernate.show_sql">true</prop>
                <!-- 格式化SQL -->
                <prop key="hibernate.format_sql">true</prop>
                <!-- 映射到DDL的自动创建 -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        
        <!-- 配置引入映射文件 -->
        <property name="mappingResources">
            <list>
                <value>cn/itcast/ssh/domain/Customer.hbm.xml</value>
            </list>
        </property>
    <!-- 配置DAO -->
    <bean id="customerDao" class="cn.itcast.ssh.dao.impl.CustomerDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</bean>
配置Spring的事务管理:
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
配置事务管理器

注解事务管理的开启
    <tx:annotation-driven transaction-manager="transactionManager"/>
在业务层添加注解@Transactional

Dao层继承HibernateDaoSupport`
Dao层使用使用 this.getHibernateTemplate()相当于获取一个session

延迟加载的问题的解决:OpenSessionInViewFilter

  <filter>
      <filter-name>OpenSessionInViewFilter</filter-name>
      <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  </filter>
 
  <filter-mapping>
      <filter-name>OpenSessionInViewFilter</filter-name>
      <url-pattern>*.action</url-pattern>
  </filter-mapping>
原文地址:https://www.cnblogs.com/sybk/p/10004727.html