Spring4_day04_SSH整合

本文详细介绍SSH(Struts2+Spring+Hibernate)框架的整合步骤,包括项目搭建、配置文件设置、类创建、页面引入等环节,并深入探讨了Struts2与Spring的两种整合方式。
  1. 整合方式一:无障碍整合

    1. SSH框架回顾

    2. SSH整合

      1.   第一步:创建项目,引入jar包

        1. Struts2的jar包
          1. D:\Javasoft\struts-2.5.16-all\struts-2.5.16\apps\struts2-showcase\WEB-INF\lib
          2. 了解Struts2包
            1. struts2-convention-plugin-2.5.16.jar:Struts2的注解开发包
            2. struts2-json-plugin-2.5.16.jar:Struts2整合AJAX的开发包
            3. struts2-spring-plugin-2.5.16.jar:Struts2整合Spring的开发包
        2. Hibernate的jar包
          1. Hibernate开发必须的包
            1. hibernate-release-5.3.5.Final\hibernate-release-5.3.5.Final\lib\required
          2. MySql驱动
          3. 日志记录
          4. 注意:Struts2和hibernate都引入了一个相同的jar包(javassist包),必须删除一个。

          5. 使用C3P0连接池需要引包:hibernate-release-5.3.5.Final\hibernate-release-5.3.5.Final\lib\optional\c3p0
        1. Spring的jar包
          1. IOC开发
          2. AOP开发
          3. JDBC模板的开发
          4. 事务管理
          5. 整合单元测试的开发
          6.  整合WEB项目
          7. 整合hibernate

  

        1. 第二步:引入配置文件 

          1. Struts的配置文件

            1.   web.xml
              1. <!-- Struts2的核心过滤器 -->
                  <filter>
                      <filter-name>struts2</filter-name>
                      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
                  </filter>
                  <filter-mapping>
                      <filter-name>struts2</filter-name>
                      <url-pattern>/*</url-pattern>
                  </filter-mapping>

                 

            2.   struts.xml
          2. Hibernate配置文件

            1. hibernate.cfg.xml
              1. 删除那个与线程绑定的session
            2. 映射文件
            3. 日志文件
          3. Spring配置文件
            1. web.xml
              1. <!-- 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>
                  <!-- Struts2的核心过滤器 -->
                  <filter>
                      <filter-name>struts2</filter-name>
                      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
                  </filter>
                  <filter-mapping>
                      <filter-name>struts2</filter-name>
                      <url-pattern>/*</url-pattern>
                  </filter-mapping>

                 

            2. applicationContext.xml
            3. 日志记录
      1. 第四步:创建相关类

      2.  

      3. 第五步:引入相关的页面

      4.  

      5. 第六步:
        1. applicationContext.xml
          1. <!-- 配置Service -->
                <bean id="customerService" class="com.lee.ssh.service.impl.CustomerServiceImpl">
                
                </bean>

             

        2. struts.xml
          1. <struts>
                  <!-- 配置struts2的常量 -->    
                 <constant name="struts.action.extension" value="action"></constant>    
                 <!-- 配置Action -->
                 <package name="ssh" extends="struts-default" namespace="/">
                     <action name="customer_*" class="com.lee.ssh.web.action.CustomerAction" method="{1}">
                     
                     </action>
                 </package>        
            </struts>

             

      6. 第八步:Spring整合struts2方式二:Action交给Spring管理

        1. 引入插件包struts2-spring-plugin-2.5.16.jar
        2. 将Action配置到Spring里
          1. applicationContext.xml
          2. <!-- 配置Action -->
                <bean id="customerAction" class="com.lee.ssh.web.action.CustomerAction">
                    
                </bean>

             

        3. 在struts.xml引入
                1. <struts>
                       <!-- 配置struts2的常量 -->    
                       <constant name="struts.action.extension" value="action"></constant>    
                       <!-- 配置Action -->
                       <package name="ssh1" extends="struts-default" namespace="/">
                           <!-- class="CustomerAction"是applicationContext.xml中的Action的id,引入的就是它 -->
                           <action name="customer_*" class="customerAction" method="{1}">
                           
                           </action>
                       </package>
                  </struts>

                   

                   

        4. 注意:原来的Action是多例的,交给Spring后默认变成单例的,需要配置Action为多例

          1. applicationContext.xml
          2. <!-- 配置Service -->
                <bean id="customerService" class="com.lee.ssh.service.impl.CustomerServiceImpl" scope="prototype">
                
                </bean>

             

        5. 注意:需要手动注入Service(applicationContext。xml)

          1. <!-- 配置Action -->
                <bean id="CustomerAction" class="com.lee.ssh.web.action.CustomerAction"  scope="prototype">
                    <property name="customerService" ref="customerService"></property>
                </bean>

             

        6. 第九步:Service调用DAO
          1. 将DAO交给Spring管理
          2. <!-- 配置DAO -->
                <bean id="customerDao" class="com.lee.ssh.dao.impl.CustomerDaoImpl">
                
                </bean>

             

          3. package com.lee.ssh.service.impl;class CustomerServiceImpl
          4. //注入DAO
                private CustomerDao customerDao;
                
                public void setCustomerDao(CustomerDao customerDao) {
                    this.customerDao = customerDao;
                }

             

        7. 第十步:Spring整合Hibernate框架
          1. 编写实体和映射
          2. 在Spring配置文件中引入Hibernate的配置信息
          3.     <!-- Spring整合Hibernate -->
                <!-- 引入Hibernate的配置信息,并根据classpath:hibernate.cfg.xml生成配置文件======= -->
                <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBuilder">
                    <!-- 引入Hibernate的配置文件 -->
                    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
                </bean>

             

          4. 在Spring和Hibernate整合后,Spring提供了一个Hibernate的模板简化Hibernate的开发
            1. 改写DAO继承HibernateDaoSupport
            2. package com.lee.ssh.dao.impl;
              
              import java.util.List;
              
              import org.hibernate.Session;
              import org.hibernate.Transaction;
              import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
              
              import com.lee.ssh.dao.CustomerDao;
              import com.lee.ssh.domain.Customer;
              import com.lee.ssh.utils.HibernateUtils;
              
              public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
              
              
                  @Override
                  public void save(Customer customer) {
                      System.out.println("CustomerDaoImpl中的save方法执行了");
                  }
                  
              }

               

            3. 在DAO中直接注入Session Factory:一旦注入,就会自动创建Hibernate模板类
            4. <!-- 配置DAO -->
                  <bean id="customerDao" class="com.lee.ssh.dao.impl.CustomerDaoImpl">
                      <property name="sessionFactory" ref="sessionFactory"></property>
                  </bean>

               

            5. 在DAO中使用Hibernate的模板完成保存操作
            6. public void save(Customer customer) {
                      System.out.println("CustomerDaoImpl中的save方法执行了");
                      this.getHibernateTemplate().save(customer);
                  }

               

        8. 第十一步:配置Spring的事务管理
          1. public void save(Customer customer) {
                    System.out.println("CustomerDaoImpl中的save方法执行了");
                    this.getHibernateTemplate().save(customer);
                }

             

          2. 开启注解事务

          3. <!-- 开启注解事务 -->
                <tx:annotation-driven transaction-manager="transactionManager"/>

             

          4. 在业务层使用注解
          5. @Transactional
            public class CustomerServiceImpl implements CustomerService {

             

          6.  

              

 

  1. 整合方式二:没有Hibernate文件,将hibernate的配置都交给Spring管理

  2. Hibernate模板的使用

  3. 延迟加载问题的解决

转载于:https://www.cnblogs.com/spring-lee/p/9636172.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值