首页 > 其他 > 详细

SSH搭建

时间:2021-03-04 10:19:10      阅读:29      评论:0      收藏:0      [点我收藏+]

SSH框架搭建

1. 搭建Spring框架

  1. 右键项目,选择MyEclipse->add Spring Cabalitiles

    技术分享图片

  2. 版本选择Spring3.1 勾选前面四个,点击finsh即可

    技术分享图片

    2. 搭建Hibernate框架

    1. 右键项目,选择MyEclipse->add Hibernation Cabalitiles(同上)

    技术分享图片

     

    2.版本选择3.3,即下图

技术分享图片

  1. 一直下一步,到下图是把左上角的勾取消,点击完成

    技术分享图片

    3.搭建struts2框架

    1.右键项目,选择MyEclipse->add Struts Cabalitiles(同上)

    技术分享图片

    1. 版本选择2.1,点击下一步

      技术分享图片

    2. 勾选与spring相关的选项,如图,点击完成

      技术分享图片

       

      4.创建实体类

      注意:无需创建abstract 类,可取消,以及选择Spring DAO,并将创建的dao放进dao包里面,一直下一步即可

      技术分享图片

       

      5.创建action并且继承ActionSupport类

      技术分享图片

      6.配置Spring框架(注入所有的DAO类、注入所有的Action类)

      1. 配置sessionFactory属性。

      2. 注入所有action类以及dao类。注意:注入的dao类必须在原来的类中添加属性以及setter方法技术分享图片

        技术分享图片

        1. 启动Spring:打开web.xml

          第一步:选择Context Parameters

          Name:contextConfigLocation Value:classpath:applicationContext.xml

          技术分享图片

          第二步:选择Listeners 查找contextLoaderLister

          技术分享图片

           

          7.配置struts

          技术分享图片

          注意:action中的class是spring配置文件中注入的id

          8.SSH框架事务管理

          1. 通过Spring配置管理Hibernate事务。

          注意:

          Spring配置事务时:

          1. 切入目标必须有接口实现;

          2. 在代码中必须要使用接口类型。

            创建接口:鼠标指向dao类 选择Refactor Extract Interface

            技术分享图片

            技术分享图片

        注意:取名最好在原来的名字前面加个大写I,并且选择需要的方法勾上即可

        1. Spring中配置事务管理

        <!--先在applicationContext.xml文件头部修改添加tx和aop空间(不修改已有的空间)-->
        <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:tx="http://www.springframework.org/schema/tx"
         xmlns:aop="http://www.springframework.org/schema/aop"
        ?
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        ?
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

        在java代码里面输入hibernatetransaction,然后查看导入的包复制即可(class中的长代码)

           <!-- 注入事务管理器 -->
          <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
          </bean>
          <!-- 注入事务管理通知器 -->
         <tx:advice id="advice" transaction-manager="transactionManager">
             <tx:attributes>
             <!-- 配置哪些方法开启事务 -->
               <tx:method name="save*" propagation="REQUIRED"/>
               <tx:method name="update*" propagation="REQUIRED"/>
               <tx:method name="delete*" propagation="REQUIRED"/>
               <!-- 配置哪些方法只读处理 -->
               <tx:method name="*" read-only="true"/>
             </tx:attributes>
         </tx:advice>
         <!-- 事务AOP配置 -->
           <!--
        创建切入点,指示切入哪些方法
        表达式:* dao.impl.*.*(..)
        意思表示:任何返回类型 dao.impl.任何类.任何方法(不限参数)
        -->
         <aop:config>
         <aop:pointcut expression="execution(* dao.*.*(..))" id="pointcut"/>
         <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
         </aop:config>

         

      扩展:消除懒加载

      技术分享图片

重点:找到OpenSessionViewFilter

第二步 在web.xml文件中配置地址

技术分享图片

注意:过滤器必须在所有dao类注入前配置好

 

SSH搭建

原文:https://www.cnblogs.com/caledown/p/14478405.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!