spring3管理struts2和hibernate3(1)

其实原理很简单,struts2负责处理页面和逻辑,位于上层,hibernate3负责持久层和数据交互,位于下层,而spring3穿插于从上至下,负责管理和配置。由于配置的地方太多,所以显得难,以前也一直没有全部拉通过,现在将大概的流程都写上来:

 

首先,我们要将spring的xml文件建立好,让它管理hibernate的持久类,也管理service,action等

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://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/aop
                      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                      http://www.springframework.org/schema/task
                      http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    <bean id="myDataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.mysql.jdbc.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://192.168.86.55:3306/ihelpdesk</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>584920</value>
        </property>
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="myDataSource"/>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLInnoDBDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.default_schema"></prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.search.autoregister_listeners">false</prop>
                <prop key="hibernate.max_fetch_depth">6</prop>
                <prop key="hibernate.jdbc.fetch.size">300</prop>
                <prop key="hibernate.jdbc.batch_size">20</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
            <value>com/po/User.hbm.xml</value>
            <value>com/po/OrderHeader.hbm.xml</value>
            </list>
        </property>

       
    </bean>

    <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

    <bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <aop:config>
        <aop:pointcut id="allServiceMethod"
                      expression="execution(* com.synnex.cloud..*.service.*Service*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allServiceMethod"/>
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="new*" propagation="REQUIRED" rollback-for="Throwable"/>
        </tx:attributes>
    </tx:advice>

    <!-- access local DB bean -->


    <import resource="applicationContext-action.xml"/>
    <import resource="applicationContext-dao.xml"/>
    <import resource="applicationContext-service.xml"/>
    <!--
    
   
    <import resource="applicationContext-security.xml"/>
     -->
</beans>

其中这三条尤为重要,

    <import resource="applicationContext-action.xml"/>
    <import resource="applicationContext-dao.xml"/>
    <import resource="applicationContext-service.xml"/>
从后缀名上可以看出,这几个xml的作用。

另外,数据库的连接地址,还有持久化类xml的路径,也同样配置在这里。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值