模拟转账操作,即Jone减少500,tom增加500
如果有疑问请访问spring事务控制-基于xml方式

1.创建数据表

2.创建Account实体类

public class Account {private String name;private String money;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getMoney() {return money;}public void setMoney(String money) {this.money = money;}
}

3.创建AccountDao接口及其实现类(接口代码省略)

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {@Autowiredprivate JdbcTemplate template;@Overridepublic void out(String outMan, double money) {template.update("update account set money=money-? where name=?",money,outMan);}@Overridepublic void in(String inMan, double money) {template.update("update account set money=money+? where name=?",money,inMan);}
}

4.创建AccountService接口及其实现类(接口代码省略)

@Service("accountService")
public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)@Overridepublic void transfer(String outMan, String inMan,double money) {accountDao.out(outMan,money);accountDao.in(inMan,money);}}

5.配置spring文件

<?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" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context  https://www.springframework.org/schema/context/spring-context.xsd"><!--    配置组件扫描--><context:component-scan base-package="com.hao"/><!--开启事务--><tx:annotation-driven transaction-manager="transactionManager"/><!--    配置平台事务管理器,当前的DataSourceTransactionManager是jdbc、mybatis技术,如果以后使用了其他技术,则此处需要修改--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--    配置数据源--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.cj.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?serverTimezone=UTC"/><property name="user" value="root"/><property name="password" value="hao20001010"/></bean>
<!--    将模板对象注入到spring容器--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean>
</beans>

6.测试:

public class AccountController {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");AccountService service= context.getBean(AccountService.class);service.transfer("Jone","tom",500);}
}

结果:

Spring的事务控制-基于注解的方式相关推荐

  1. Spring的事务控制-基于xml方式

    介绍:该程序模拟了转账操作,即Jone减少500元,tom增加500元 1.导入坐标 <dependency><groupId>junit</groupId>< ...

  2. Spring配置形式之基于注解的方式

    在classpath中扫描组件 组件扫描(component scanning): Spring 能够从 classpath 下自动扫描, 侦测和实例化具有特定注解的组件. 特定组件包括: @Comp ...

  3. 【Spring】IOC:基于注解的IOC容器初始化源码分析

    从 Spring2.0 以后的版本中,Spring 也引入了基于注解(Annotation)方式的配置,注解(Annotation)是 JDK1.5 中引入的一个新特性,用于简化 Bean 的配置,可 ...

  4. Spring基于注解的方式一

    Spring基于注解的方式一 Spring注解简介 之前的时候我们学习的Spring都是基于Spring配置文件的形式来编写,现在很多的情况下使用SpringBoot的时候是基于注解的形式,这里我们首 ...

  5. spring支持事务管理的两种方式

    转载:https://blog.csdn.net/bao19901210/article/details/41724355 事物管理对于企业应用来说是至关重要的,好使出现异常情况,它也可以保证数据的一 ...

  6. 【Spring】事务控制API

    Spring事务控制需要明确 1. JavaEE体系进行分层开发,事务处理位于业务层,Spring提供了分层设计==业务层==的事务处理解决方案. 2. Spring框架提供了一组事务控制的接口.在S ...

  7. spring中事务控制的一组API

    Spring事务控制我们要明确的 第一:JavaEE体系进行分层开发,事务处理位于业务层,Spring提供了分层设计业务层的事务处理解决方案. 第二:spring框架为我们提供了一组事务控制的接口.具 ...

  8. Spring MVC 中的基于注解的 Controller

    为什么80%的码农都做不了架构师?>>>    Spring MVC 中的基于注解的 Controller @Controller 基于注解的 Controller   终于来到了基 ...

  9. 基于注解的方式实现分布式锁

    基于注解的方式实现分布式锁 关于分布式锁的实现由两种 基于redis 基于zookeeper 为了方便分布式锁的使用, 基于注解的方式抽取成公用组件 DisLock注解 /*** 分布式锁的注解, 通 ...

最新文章

  1. python输入输出-6、Python 输入输出
  2. Spring BeanFactoryPostProcessor接口详细使用
  3. pandas对每十行做批量操作_pandas批量处理数据
  4. 大剑无锋之HTTP连接、Tcp三次握手四次挥手、Tcp状态
  5. 对jvm 同步锁的理解
  6. 成功创业者所需的能力
  7. Oracle序列更新为主键最大值
  8. Python ' ~ ' (取反) 操作符解释
  9. ajax提交整个form表单
  10. 利用awstats分析nginx日志 简单配置
  11. 算法面试题解答(六)
  12. linux curl post/put请求
  13. 新增一个主键自增长_第17期:索引设计(主键设计)
  14. C++RAII惯用法:C++资源管理的利器
  15. 从零开始学习音视频编程技术
  16. 无感支付及相应技术规范
  17. c语言水表程序流程图,水表检定操作流程图.pdf
  18. 解决virtualbox导入虚拟机报错E_INVALIDARG (0x80070057)问题
  19. 一键圣诞帽 html5源码,HTML5在线教程之微信小程序“圣诞帽”的实现思路详解
  20. Failed to open /var/lib/samba/private/secrets.tdb

热门文章

  1. POJ 2301 Beat the Spread!
  2. (Prototype)原型模式的Java实现(转)
  3. PhotoshopCS6-视觉特效插画技法-15-磨砂金属效果分析
  4. 周末加班重构代码的几点感慨
  5. [转]gtest使用
  6. 西南交大计算机专硕就业怎么样,国内四所交通大学,有985也有211,就业、深造容易,值得报考...
  7. keep alive PHP,vue中keep-alive使用方法详解
  8. java 将图片转成二进制文件bin_java 问题:怎样把一个bin二进制图片文件用java代码打开?求解!...
  9. flink state ttl
  10. python concat去除重复值语句_Python DataFrame使用drop_duplicates()函数去重(保留重复值,取重复值)...