ssh框架整合步骤

  • ssh搭建
    • struts2搭建
      • 1、略
    • spring集成到struts2项目中
      • 1、拷贝spring的jar包,到struts2项目中的lib文件夹下,同时到struts下拷贝加struts2-spring-plugin到工程中
      • 2、在web.xml中,添加一个listener,使用的类是spring提供的:ContextLoaderListener
      • 3、在web.xml中,配置context-param   ,param-name:contextConfigLocation,param-value:class path:spring-*.xml
      • 4、创建一个spring-test.xml文件,在classes中,同时创建一个bean标签,该标签指代struts中所创建的action类:<bean id=“testAction”  class=“com.lanou.ssh.controller.TestAction” scope=“prototype”></bean>
      • 5、修改struts.xml,添加content标签,name=struts.objectFactory
      • 6、修改struts.xml中配置的action,将action标签中的class内容改成spring中配置的action的id 例:<action name=“*” class=“testAction” method=“{1}”>

      • 7、如果Action中写了service,需要在spring配置文件中,添加对应的service的bean:例:<bean id=“testservice” class=“com.lanou.service.TestAction”></bean> 并且需要将这个对象添加到action的bean中(DI)

      • 将hibernate添加到ss框架中
          • 1、将hibernate的夹包拷贝到工程的lib下,同时拷贝jdbc的jar包
          • 2、在spring中,添加需要配置DataSource,我们以c3p0为例
            • <bean id=“dadaSource” class=“com.mchange.v2.c3p0.ComboPooledDataSource”></bean>
            • 配置子标签:driverClass:XXX,jdbcUrl:XXXXX
            • 需要配置session factory,我们以hibernate5支持包中的LocalSessionFactoryBean为例
              • dataSource:就是刚才配置的datasource,使用ref引入这个
              • hibernateProperties:将hibernate配置文件中,在数据元里面没有出现的所有内容,添加到这条配置里(props)
              • mappingLocations设置mapping文件的位置:classpath:com/XXXXXXX
            • 配置我们写的dao,将sessionfactory作为属性通过DI注入dao中
          • 3、写一个dao出来

          • struts.xml 配置文件
          • <?xmlversion="1.0"encoding="UTF-8"?>

            <!DOCTYPE strutsPUBLIC

            "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

            "http://struts.apache.org/dtds/struts-2.3.dtd">


            <struts>

               

                <constantname="struts.enable.DynamicMethodInvocation"value="false"/>

                

                <constantname="struts.devMode"value="true"/>

                 <constantname="struts.objectFactory"value="spring"/>

                 

                <packagename="default"namespace="/user"extends="struts-default">

               <actionname="user_*"  class="com.lanou.ssh.controller.UserAction"method="{1}">

               <result name="success">

               /success.html

               </result>

               <resultname="error">

               /error.html

               </result>

               </action>

                </package>


          • </struts>

          • spring-test.xml配置文件

          • <?xmlversion="1.0"encoding="UTF-8"?>

            <beansxmlns="http://www.springframework.org/schema/beans"

            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"

            xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"


            xsi:schemaLocation="

                   http://www.springframework.org/schema/beans 

                   http://www.springframework.org/schema/beans/spring-beans-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

                   http://www.springframework.org/schema/aop 

                   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


            <!--配置service -->

            <beanid="userService"class="com.lanou.ssh.services.impl.UserService">

            <propertyname="userDao"ref="userDao"></property>

            </bean>

            <!-- 配置action -->

            <beanid="UserAction"class="com.lanou.ssh.controller.UserAction"

            scope="prototype">

            <propertyname="userService"ref="userService"></property>

            </bean>

            <!--配置到dao -->

            <beanid="userDao"class="com.lanou.ssh.dao.impl.UserDao">

            <propertyname="sessionFactory"ref="sessionFactory"></property>

            </bean>

            <!-- 配置sessionFactory,供dao用 -->

            <beanid="sessionFactory"

            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

            <!--注入数据源,-->

            <propertyname="dataSource"ref="dataSource"></property>

            <!-- 配置别的属性 -->

            <propertyname="hibernateProperties">

            <props>

            <propkey="show_sql">true</prop>

            <propkey="hibernate.hbm2ddl.auto">update</prop>

            <propkey="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>

            <propkey="hibernate.current_session_context_class">thread</prop>

            </props>

            </property>

            <!--hibernate的映射文件路径 -->

                 <propertyname="mappingLocations" value="classpath:com/lanou/ssh/entity/mappings/*.hbm.xml"></property>

            </bean>

            <!--以下是c3p0的配置文件 ,供sessionFactory用 -->

            <beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"

            destroy-method="close">

            <propertyname="driverClass"value="com.mysql.jdbc.Driver"/>

            <propertyname="jdbcUrl"

            value="jdbc:mysql://localhost:3306/ssh?characterEncoding=utf-8&amp;useSSL=false"/>

            <propertyname="user"value="root"/>

            <propertyname="password"value="root"/>

            <propertyname="maxPoolSize"value="30"></property>

            <propertyname="minPoolSize"value="5"/>

            <propertyname="acquireIncrement"value="1"></property>

            </bean>


            </beans>

            user.hbm.xml配置文件

    • <?xmlversion="1.0"encoding="UTF-8"?>


      <!DOCTYPEhibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

      <hibernate-mapping>


         <class name="com.lanou.ssh.entity.User" table="tb_user">

           <id name="id" >

          

              <generatorclass="uuid"></generator>

           </id>

             <propertyname="username" ></property>

             <propertyname="pwd" ></property>

         </class>


      </hibernate-mapping>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞腾创客

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值