SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务
来源:互联网 发布:屏蔽网络视频 编辑:程序博客网 时间:2024/05/21 22:57
- SSH与SSM学习之Spring23Spring事务之注解配置方式管理事务
- 一导包
- 二AccountSeviceImpl类
- 三配置配置文件
- 1 开启注解管理事务
- 2 完整配置
- 四测试
- 五源码下载
SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务
一、导包
Spring AOP包
“`xml
spring-aop
spring-aspects
org.aspectj.weaver
org.aopalliance
“`
二、AccountSeviceImpl类
Dao接口以及实现、Service接口和之前的是一样的,AccountSeviceImpl不一样,如下
事务的注解可以注解到类上,也可以注解到方法上,可以两者都注解。
package com.qwm.spring3.tx.service;import com.qwm.spring3.tx.dao.AccountDao;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.annotation.Isolation;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import org.springframework.transaction.support.TransactionCallback;import org.springframework.transaction.support.TransactionCallbackWithoutResult;import org.springframework.transaction.support.TransactionTemplate;/** * @author:qiwenming * @date:2017/10/31 0031 21:02 * @description: */@Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,readOnly = false)public class AccountServiceImpl implements AccountService { private AccountDao ad; private TransactionTemplate tt; @Override @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,readOnly = false) public void updateMoney(Long fromId, Long toId, Double money) { //减钱 ad.decreaseMoney(fromId,money);// int i = 1/0; //加钱 ad.increaseMoney(toId,money); } public void setAd(AccountDao ad) { this.ad = ad; } public void setTt(TransactionTemplate tt) { this.tt = tt; }}
三、配置配置文件
3.1 开启注解管理事务
<!-- 开启使用注解管理aop事务 --> <tx:annotation-driven/>
3.2 完整配置
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> <!-- 1.指定spring读取db.properties配置--> <context:property-placeholder location="classpath:com/qwm/spring3/db.properties"/> <!--2.将连接池对象放入到spring容器中--> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 3.将AccountDao放入到spring 容器中 --> <bean name="accountDao" class="com.qwm.spring3.tx.dao.AccountDaoImpl"> <property name="dataSource" ref="dataSource"/> </bean> <!--事务核心管理器,封装了所有事务操作,依赖于连接池--> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--事务模板对象--> <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"> <property name="transactionManager" ref="transactionManager"/> </bean> <!-- 开启使用注解管理aop事务 --> <tx:annotation-driven/> <!--4.将AccountService放入到spring容器中--> <bean name="accountService" class="com.qwm.spring3.tx.service.AccountServiceImpl"> <property name="ad" ref="accountDao"/> <property name="tt" ref="transactionTemplate"/> </bean></beans>
四、测试
测试之前,先把数据库中表的数据恢复到初始值
测试代码
/** * @author:qiwenming * @date:2017/10/31 0031 21:32 * @description: */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:com/qwm/spring3/tx/applicationContext.xml")public class Demo { @Resource(name = "accountService") private AccountService as; @Test public void test1(){ as.updateMoney(1L,2L,100D); }}
结果
五、源码下载
https://github.com/wimingxxx/spring01/tree/master/src/com/qwm/spring3/tx
阅读全文
0 0
- SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务
- SSH与SSM学习之Spring22——Spring事务之xml配置方式管理事务
- SSH与SSM学习之Spring21——Spring事务之代码方式管理事务(基本不用)
- SSH与SSM学习之Spring19——Spring事务之事务基础
- SSH与SSM学习之Spring20——Spring事务之Spring相关的基础
- SSH与SSM学习之SSH整合09——Spring的aop事务
- SSH与SSM学习之Spring17——Spring中AOP之使用注解配置
- spring配置事务之注解方式
- SSH与SSM学习之Spring09——Spring使用注解配置
- SSH与SSM学习之hibernate12——hibernate中的事务
- Spring注解方式管理事务
- SSH与SSM学习之SSH整合02——单独配置Spring容器
- SSH与SSM学习之Spring16——Spring中AOP之使用配置文件配置
- SSH与SSM学习之Spring03——Spring创建对象的方式
- Spring学习笔记(15)----使用Spring注解方式管理事务
- Spring学习笔记(17)----使用Spring注解方式管理事务
- Spring学习笔记(17)----使用Spring注解方式管理事务
- Spring 使用Spring注解方式管理事务与传播行为
- 作业
- Android学习-通知的使用详解
- Mac OS上使用Electron
- 【常用函数封装】php-数字
- WP_Query的使用方法
- SSH与SSM学习之Spring23——Spring事务之注解配置方式管理事务
- web.xml报错
- 作业
- java作业2
- 数据结构实验之二叉树三:统计叶子数
- Vijos 3747 随机图
- PAT --- 1017. A除以B (20)
- 作业
- 2、安装Oracle数据库