xml声明式事务控制
1、applicationContext.xml配置
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--组件扫描 --> <context:component-scan base-package="com.itheima"/> <!--加载properties文件--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--配置数据源信息--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置SqlSessionFactoryBean,作用将SqlSessionFactory存储到spring容器--> <bean class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> </bean> <!--MapperScannerConfigurer,作用扫描指定的包,产生Mapper对象存到spring容器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itheima.mapper"></property> </bean> <!--配置平台事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置spring提供好的advice--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 配置不同的方法的事务属性 name:方法名称 *代表通配符 添加操作addUser、addAccount =>add* isolation: 事务的隔离级别,解决事务并发问题 timeout: 超时时间 默认-1永不超时 单位是秒 read-only: 是否只读 propagation:事务的传播行为,解决业务方法调用业务方法(事务嵌套问题) 默认 REQUIRED(默认值) --> <tx:method name="*" isolation="READ_COMMITTED" timeout="3" read-only="true" propagation="MANDATORY"/> </tx:attributes> </tx:advice> <!--事务增强aop--> <aop:config> <!--配置切点表达式--> <aop:pointcut id="txPointcut" expression="execution(* com.itheima.service.impl.*.*(..))"/> <!--配置织入关系 通知advice-ref引入spring提供好的--> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> </beans>
2.、spring事务控制,注解声明式事务控制
<!--事务的自动代理(注解驱动)--> <tx:annotation-driven/>
@Service("accountService") @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED) public class AccountServiceImpl implements AccountService { @Autowired private AccountMapper accountMapper; @Override @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED) public void transferMoney(String outAccount, String inAccount, Integer money) { accountMapper.incrMoney(outAccount, money); accountMapper.decrMoney(inAccount, money); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术