SSH整合的配置文件

配置数据源

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/数据库?useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
        <property name="initialSize" value="3"/>
        <property name="maxActive" value="500"/>
        <property name="maxIdle" value="2"/>
        <property name="minIdle" value="1"/>
</bean>

配置sessionFactory

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource"/>
     <property name="mappingResources">
        <list>
          <value>cn/xx/bean/Person.hbm.xml</value>
        </list>
     </property>
     <property name="hibernateProperties">
        <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=false
                hibernate.format_sql=false        
        </value>
    </property>
</bean>


在struts-config文件中, 配置这个之后,Struts就会被Spring接管

<!-- 配置代理请求处理 DelegatingRequestProcessor -->
<controller>
   <set-property value="org.springframework.web.struts.DelegatingRequestProcessor" property="processorClass"/>
</controller>

然后接着在Spring容器中配置Action

<!-- 配置action -->
<bean class="com.haitao.web.action.UserAction" name="/user" scope="prototype">
   <property name="" ref=""/>
</bean>


在web.xml文件中加入如下Filter,解决中文乱码问题

<filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

加入下面的Filter,解决懒加载问题

<filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>

spring容器中启动事务管理器,如果某个Bean需要用到事务,只需要在bean类前加 @Transactional ,加上之后,该类中用到的所有的事务都被Spring的事务管理器接管了

<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>


加上 下面的注解方式,同时在bean类的属性前面加上@Resource,就会启用byName方式为这个属性自动装配

<!-- 启用注解方式 -->
<context:annotation-config/>

配置Service


<!-- 配置Service -->
<bean class="com.haitao.services.imp.UserService" id="userService">
   <property name="sessionFactory" ref="sessionFactory"/>
</bean>

Web容器初始化Spring容器,需要在web.xml文件中添加如下配置

  <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>



FR:海涛高软(hunk Xu)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值