ssh整合资料

来源:互联网 发布:淘宝买家退回货物不符 编辑:程序博客网 时间:2024/06/05 04:02

SSH整合ssh整合资料(自己整理供参考)

Struts(表示控制层)+Spring(业务层)+Hibernate(持久层)

一、Spring与Struts整合

<一>前提:必须在WEB应用启动时,创建Spring的ApplicationContext实例

方式1:采用ContextLoaderListener来创建ApplicationContext

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/spring-config/applicationContext.xml<param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

<listener>

方式2:采用ContextLoaderPlugIn来创建ApplicationContext

<plug-inclassName=”org.springframework.web.struts.ContextLoaderPlugIn”>

<set-property property=”contextConfigLocation”value=”/WEB-INF/config/applicationContext.xml”/>

</plug-in>

<二>SS整合

方式1:通过Spring的DelegatingActionProxy类(步骤)

1.      在Action中,使用IOC获得服务;

2.      在struts配置文件中(struts-config.xml)

<action  path=”/new”type=”org.springframework.web.struts.DelegatingActionProxy”/>

3.      在Spring配置文件中(applicationContext.xml)

<bean name=”/new”class=”SomeAction”>

<property name=”service”ref=”Service”/>

</bean>

优点:不使用Spring API编写Action

     利用了IOC装配

     可以利用容器的scope=”prototype”来保证每一个请求有一个单独的Action来处理,避免struts中的Action的线程安全问题

缺点:struts配置文件中,所有Path都映射到同一个代理类

方式2:通过Spring的DelegatingRequestProcessor类(步骤)

1.      在Action中,使用IOC获得服务

2.      在Struts配置文件中(struts-config.xml)

<controllerprocessorClass=”org.springframework.web.struts.DelegatingRequestProcessor”/>

3.      在Spring配置文件中(applicationContext.xml)

<bean name=”/new”class=”SomeAction”>

<property name=”service”ref=”Service”/>

</bean>

二、Spring与Hibernate的整合

<一> 数据源的配置

方式1:采用Spring内置的数据源DriverManagerDataSource

  <bean id=”dataSource”class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>

<propertyname=”driverClassName” value=”com.mysql.jdbc.Driver”/>

<property name=”url”value=”jdbc:mysql://localhost:3306/hisoft”/>

<property name=”username”value=”root”/>

<property name=”password”value=”123456”/>

</bean>

方式2:采用开源数据库产品DBCP

<bean id=”dataSource”class=”org.apache.commons.dbcp.BasicDataSource”>

<property name=”driverClassName”value=”com.mysql.jdbc.Driver”/>

<property name=”url”value=”jdbc:mysql://localhost:3306/hisoft”/>

<property name=”username”value=”root”/>

<property name=”password”value=”123456”/>

</bean>

方式3:直接使用容器提供的数据源(如Tomcat)JNDI

Step1:在server.xml中:

      <Resource  name=”jdbc/mydatasource”auth=”Container” description=”DBConnection”

        type=”javax.sql.DataSource” username=”root” password=”123456”

        driverClassName=”com.mysql.jdbc.Driver”

        url=”jdbc:mysql://localhost:3306/hisoft”

maxActive=”5”/>

step2: 在context.xml中(conf/context.xml):

      <ResourceLink name=”jdbc/mydatasource”

global=”jdbc/mydatasource”

type=”javax.sql.DataSource”/>

step3: 在beans-config.xml中:

      <bean id=”dataSource”class=”org.springframework.jndi.jndiObjectFactoryBean”>

       <property name=”jndiName”value=”java:comp/env/jdbc/mydatasource”/>

      </bean>

<二>Spring对Jdbc与Hibernate的支持

方式1:JdbcTemplate模板配置

       <bean id=”jdbcTemplate”class=”org.springframework.jdbc.core.JdbcTemplate”>

          <property name=”dataSource”ref=”dataSource”/>

       </bean>

方式2:HibernateTemplate模板配置

 Step1:<bean id=”sessionFactory”class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>

     <property name=”dataSource”ref=”dataSource”/>

     <propertyname=”mappingResources”>

      <list>

       <value>lab6/order.hbm.xml</value>

      </list>

</property>

<propertyname=”hibernateProperties”>

<props>

<prop key=”hibernate.dialect”>

Org.hibernate.dialect.MySQLDialect

</prop>

<propkey=”hibernate.show_sql”>true</prop>

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

<propkey=”hibernate.format_sql”.true</prop>

</props>

</property>

</bean>

Step2:<bean id=”hibernateTemplate”class=”org.springframework.orm.hibernate3.HibernateIemplate”>

<property name=”sessionFactory”ref=”sessionFactory”/>

</bean>

<三>spring事务管理

   <bean id="transactionManager"

       class="org.springframework.orm.hibernate3.HibernateTransactionManager">

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

   </bean>

   <tx:advice id="txAdvice"transaction-manager="transactionManager">

       <tx:attributes>

           <tx:method name="*" propagation="REQUIRED"/>

       </tx:attributes>

   </tx:advice>

   <aop:config>

    <aop:advisor pointcut="execution(*allan.biz.*.*(..))" advice-ref="txAdvice" />

   </aop:config>