关于spring+ibatis配置事务

为了配置事务,在网上找了很多方法,几乎都是一样,心想应该不会有问题,照着网上一步一步来,最后失败一次又一次.没有办法.后来终于发现网上大多数都少了配置参数,废话一堆,直接上配置文件,应该一看就明白.
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://10.11.0.145:3306/carrefour?characterEncoding=gb2312" />
<property name="username" value="dev01" />
<property name="password" value="123456" />
</bean>
<bean id="sqlClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation">
<value>classpath:sqlmaps.xml</value>
</property>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置哪些方法,什么情况下需要回滚-->
<tx:advice id="serviceAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<!--当代理的service层中的方法抛出异常的时候才回滚,必须加rollback-for参数-->
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable"/>
<!--除了上面标识的方法,其他方法全是只读方法-->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointcut" expression="execution(* com.crm.service.*.*(..))"/>
<aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice"/>
</aop:config>
<!--order配置 -->
<import resource="report/report.xml" />
<import resource="myfaq/myfaq.xml" />
<import resource="order/order.xml" />

<!--user配置-->
<import resource="user/user.xml" />
<!--faq配置-->
<import resource="faq/faq.xml" />
<!-- sales配置 -->
<import resource="sales/sales.xml" />
<import resource="trailcase/trailcase.xml" />
<import resource="help/help.xml" />
</beans>


附:
<1>如果是mysql数据库,一定把数据库的引擎改为innodb
<2>附件的内容为aop需要的额外jar包
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值