首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring治理iBatis事务

2013-04-09 
Spring管理iBatis事务Spring管理iBatis事务?sqlMapConfigsqlMap resourcecom/angi/ibatis/maps/User.

Spring管理iBatis事务
Spring管理iBatis事务

?

<sqlMapConfig>    <sqlMap resource="com/angi/ibatis/maps/User.xml" /></sqlMapConfig>

?

?

?以上配置省去了transactionManager的配置,就会使用external(外部)事务管理(ExternalTransaction),即等同如下配置:

?

<sqlMapConfig>    <transactionManager type="EXTERNAL">                <!--这个数据源其实没有什么意义,还是取上面的省略方式吧-->        <dataSource type="DBCP">        </dataSource>    </transactionManager>    <sqlMap resource="com/angi/ibatis/maps/User.xml" /></sqlMapConfig>

?

1、TransactionProxyFactoryBean

?

<?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:p="http://www.springframework.org/schema/p"    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-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">    <!-- DataSource -->    <bean id="dataSource" value="false"/>-->        <property name="url">            <value>jdbc:mysql://localhost/test</value>        </property>        <property name="username">            <value>root</value>        </property>        <property name="password">            <value>mysql</value>        </property>    </bean>    <!-- Spring iBatis Template -->    <bean id="sqlMapClient" value="SqlMapConfig.xml" />        <property name="dataSource" ref="dataSource" />    </bean>    <bean id="transactionManager"        />        </property>    </bean>    <bean id="userDAO" />        </property>    </bean>    <bean id="userService" />        </property>    </bean>    <bean id="userServiceProxy"        />        </property>        <property name="target">            <ref local="userService" />        </property>        <property name="transactionAttributes">            <props>                <!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 -->                <prop key="*">PROPAGATION_REQUIRED</prop>            </props>        </property>    </bean></beans>

?

?

2、TransactionInterceptor

?

<?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:p="http://www.springframework.org/schema/p"    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-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">    <!-- DataSource -->    <bean id="dataSource" value="false"/>-->        <property name="url">            <value>jdbc:mysql://localhost/test</value>        </property>        <property name="username">            <value>root</value>        </property>        <property name="password">            <value>mysql</value>        </property>    </bean>    <!-- Spring iBatis Template -->    <bean id="sqlMapClient" value="SqlMapConfig.xml" />        <property name="dataSource" ref="dataSource" />    </bean>    <bean id="transactionManager"        />        </property>    </bean>    <bean id="userDAO" />        </property>    </bean>    <bean id="userService" />        </property>    </bean>    <bean        ref="transactionManager" />        <property name="transactionAttributes">            <props>                <!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 -->                <prop key="*">PROPAGATION_REQUIRED</prop>            </props>        </property>    </bean></beans>

?

?

3、AOP和TX配置

<?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:p="http://www.springframework.org/schema/p"    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-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">    <!-- DataSource -->    <bean id="dataSource" value="SqlMapConfig.xml" />        <property name="dataSource" ref="dataSource" />    </bean>    <bean id="transactionManager"        ref="dataSource" />    </bean>    <!-- 需要引入aop的命名空间 -->    <aop:config>        <!-- 切入点指明了在所有方法产生事务拦截操作 -->        <aop:pointcut id="serviceMethods"            expression="execution(* com.angi.ibatis.service.*.*(..))" />        <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />    </aop:config>    <!-- 需要引入tx的命名空间 -->    <!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 -->            <tx:method name="*" propagation="REQUIRED" />        </tx:attributes>    </tx:advice>    <bean id="userDAO" />        </property>    </bean>    <bean id="userService" />        </property>    </bean></beans>

?

?

?

4、anotation

<?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:p="http://www.springframework.org/schema/p"    xmlns:tx="http://www.springframework.org/schema/tx"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">    <!-- 需要引入tx的命名空间 -->    <tx:annotation-driven transaction-manager="transactionManager" />    <!-- DataSource -->    <bean id="dataSource" value="false"/>-->        <property name="url">            <value>jdbc:mysql://localhost/test</value>        </property>        <property name="username">            <value>root</value>        </property>        <property name="password">            <value>mysql</value>        </property>    </bean>    <!-- Spring iBatis Template -->    <bean id="sqlMapClient" value="SqlMapConfig.xml" />        <property name="dataSource" ref="dataSource" />    </bean>    <bean id="transactionManager"        />        </property>    </bean>    <bean id="userDAO" />        </property>    </bean>    <bean id="userService" />        </property>    </bean></beans>

?

?

热点排行