ssh2整合

本文详细介绍了如何在项目中整合Spring、Hibernate和Struts,并提供了关键配置步骤及代码示例,包括添加相关Jar包、配置Spring的组件扫描、数据源、SessionFactory以及事务管理等,同时整合Struts所需的配置项,如web.xml、context参数和过滤器设置。
  1. 整合hibernate
    1. 增加相关jar包
    2. 增加spring配置文件:applicationContext.xml,文件内容如下:
      1. 配置组件扫描。让spring管理basePackage和他的子包下所有的类。

<context:component-scanbase-package="com.ll"></context:component-scan>

  1. 配置dataSource数据源

<beanid="ds" class="org.apache.commons.dbcp.BasicDataSource">

<propertyname="driverClassName" value="com.mysql.jdbc.Driver">

</property>

<propertyname="url"value="jdbc:mysql://localhost:3306/test"></property>

<propertyname="username" value="root"></property>

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

</bean>

  1. 配置sessionFactory

<beanid="sessionFactory"

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

<propertyname="packagesToScan" >

<list>

<value>com.ll.po</value>

</list>

</property>

<propertyname="hibernateProperties">

<props>

<propkey="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

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

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

</props>

</property>

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

</bean>

  1. 配置事务管理(事务增加在service层!)

<beanid="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

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

</bean>

<tx:annotation-driventransaction-manager="txManager" />

  1. 配置HibernateTemplate

<beanid="hibernateTemplate"class="org.springframework.orm.hibernate3.HibernateTemplate">

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

</bean>

  1. 整合struts
    1. 导入jar包
    2. 配置web.xml
      1. 配置<context-param>指定spring配置文件的路径和名字!

<context-param>

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

<param-value>classpath:applicationContext*.xml</param-value>

</context-param>

<listener>

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

</listener>

  1. 配置opensessionInview过滤器(一定要配置在struts过滤器之前)

 <filter> 

    <filter-name>hibernateSessionFilter</filter-name> 

     <filter-class> 

 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter  

    </filter-class> 

 </filter> 

 <filter-mapping> 

    <filter-name>hibernateSessionFilter</filter-name> 

    <url-pattern>/*</url-pattern> 

 </filter-mapping>

  1. 配置struts过滤器

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

 

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

  1. 增加spring上下文加载的监听器
  1. 增加struts.xml:

增加<constant name="struts.objectFactory" value="spring" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值