| public void accountMoney() { |
| try { |
| |
| |
| |
| userDao.reduceMoney(); |
| |
| int i = 10/0; |
| |
| userDao.addMoney(); |
| |
| }catch(Exception e) { |
| |
| } |
| } |
| # bean.xml中配置事务管理器 |
| |
| <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> |
| |
| <property name="dataSource" ref="dataSource"></property> |
| </bean> |
| |
| # 创建名称空间 |
| <?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:context="http://www.springframework.org/schema/context" |
| xmlns:tx="http://www.springframework.org/schema/tx" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd |
| http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> |
| |
| # 开启事务注解 |
| <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> |
| |
| # 为业务类或方法添加注解 |
| @Transactional |
| |
| # 注意 |
| 如果把这个注解添加类上面,这个类里面所有的方法都添加事务 |
| 如果把这个注解添加方法上面,为这个方法添加事务 |
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术