spring ibatis 事物配置

可紊 提交于 2020-02-25 05:36:52

<?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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
    <!-- 数据源配置 -->
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"
            value="net.sourceforge.jtds.jdbc.Driver">
        </property>
        <property name="url"
            value="jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbName;SelectMethod=cursor">
        </property>
        <property name="username" value="sa"></property>
        <property name="password" value="sa"></property>
    </bean>
    <!-- Spring iBatis Template -->
    <bean id="sqlMapClient"
        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="classpath:config/SqlMapConfig.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 声明式事务管理 -->
    <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
        <property name="transactionManager" ref="transactionManager"></property>
        <property name="transactionAttributes">
            <props>
                <prop key="add*">PROPAGATION_REQUIRED</prop>           
                <prop key="edit*">PROPAGATION_REQUIRED</prop>
                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>
  <!-- 注意该DAO 一定要继承SqlMapClientDaoSupport 使用getSqlMapClientTemplate()方法,并且要抛出 throws DataAccessException 异常 spring才能捕获并回滚 -->
    <bean id="UserDAO" class="com.gong.struts.test.UserDAO">
        <property name="sqlMapClient" ref="sqlMapClient"></property>
    </bean>
    <bean id="UserManager" parent="baseTransactionProxy">
        <property name="target">
            <bean class="com.gong.struts.test.UserManager">
                <property name="userdao" ref="UserDAO"></property>
            </bean>
        </property>
    </bean>
</beans>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!