xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
<tx:annotation-driven/>
<!-- 5)配置事务管理器 id="transactionManager" id名必须是transactionManager,不能更改 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
/** 事务注解(@Transactional): 加在方法上,表示此方法受事务管理(自动提交,回滚事务) 加在类上,那么这个类中的所有方法都受事务管理 最后要注意的是,在业务方法中不要自己try-catch捕获异常,否则spring无法自动回滚事务 */ @Transactional public void business1(){ productMapper.delete(100000005); productMapper.delete(100000006); int i = 1/0; productMapper.delete(100000007); }
