一、创建数据库表

表名:account

字段:(`id`,`username`,`money`)

二、dao、service层创建业务接口、类

1 public interface UserDao {
2 void addMoney();
3 void reduceMoney();
4 }
 1 @Repository
2 public class UserDaoImpl implements UserDao {
3
4 @Autowired
5 private JdbcTemplate jdbcTemplate;
6
7 @Override
8 public void addMoney() {
9 String sql = "update account set money = money + ? where username = ?";
10 jdbcTemplate.update(sql,101,"zhangsan");
11 }
12
13 @Override
14 public void reduceMoney() {
15 String sql = "update account set money = money - ? where username = ?";
16 jdbcTemplate.update(sql,102,"lisi");
17 }
18 }

三、注解形式实现事务管理

 1 @Service
2 @Transactional
3 public class UserService {
4
5 @Autowired
6 UserDao userDao;
7
8 public void accountMoney() {
9 userDao.reduceMoney();
10 // 模拟异常
11 int i = 10 / 0;
12 userDao.addMoney();
13 }
14 }

bean.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
9 ">
10 <!-- 数据库连接池 -->
11 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
12 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
13 <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
14 <property name="username" value="root"></property>
15 <property name="password" value="root"></property>
16 </bean>
17 <!-- JdbcTemplate对象 -->
18 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
19 <!--注入dataSource-->
20 <property name="dataSource" ref="dataSource"></property>
21 </bean>
22 <!-- 组件扫描 -->
23 <context:component-scan base-package="com.example"></context:component-scan>
24
25 <!-- 创建事务管理器 -->
26 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
27 <!--注入数据源-->
28 <property name="dataSource" ref="dataSource"></property>
29 </bean>
30 <!-- 开启事务注解 -->
31 <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
32 </beans>

四、XML配置文件实现声明式事务管理

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:tx="http://www.springframework.org/schema/tx"
6 xmlns:aop="http://www.springframework.org/schema/aop"
7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
10 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
11 ">
12
13 <!-- 数据库连接池 -->
14 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
15 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
16 <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
17 <property name="username" value="root"></property>
18 <property name="password" value="root"></property>
19 </bean>
20
21 <!-- JdbcTemplate对象 -->
22 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
23 <!--注入dataSource-->
24 <property name="dataSource" ref="dataSource"></property>
25 </bean>
26
27 <!-- 组件扫描 -->
28 <context:component-scan base-package="com.example"></context:component-scan>
29
30 <!-- 1. 创建事务管理器 -->
31 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
32 <!--注入数据源-->
33 <property name="dataSource" ref="dataSource"></property>
34 </bean>
35
36 <!-- 2. 配置通知 -->
37 <tx:advice id="txadvice">
38 <!-- 配置事务参数 -->
39 <tx:attributes>
40 <!-- 指定哪种规则的方法上面添加事务 -->
41 <tx:method name="accountMoney" propagation="REQUIRED"/>
42 <!--<tx:method name="account*"/>-->
43 </tx:attributes>
44 </tx:advice>
45
46 <!-- 3. 配置切入点和切面 -->
47 <aop:config>
48 <!-- 配置切入点 -->
49 <aop:pointcut id="pt" expression="execution(* com.example.demo.service.UserService.*(..))"/>
50 <!-- 配置切面 -->
51 <aop:advisor advice-ref="txadvice" pointcut-ref="pt"></aop:advisor>
52 </aop:config>
53
54 </beans>

五、完全注解实现事务管理

使用配置类代替xml文件

dao、service 使用步骤二、三中的例子

 1 @Configuration // 配置类
2 @ComponentScan(basePackages = "com.example") // 扫描包
3 @EnableTransactionManagement //开启事务
4 public class TxConfig {
5
6 /**
7 * 创建数据库连接池
8 */
9 @Bean
10 public DruidDataSource getDruidDataSource() {
11 DruidDataSource dataSource = new DruidDataSource();
12 dataSource.setDriverClassName("com.mysql.jdbc.Driver");
13 dataSource.setUrl("jdbc:mysql://localhost:3306/test");
14 dataSource.setUsername("root");
15 dataSource.setPassword("root");
16 return dataSource;
17 }
18
19 /**
20 * 创建JdbcTemplate对象
21 */
22 @Bean
23 public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
24 // 到IOC容器中根据类型找到dataSource
25 JdbcTemplate jdbcTemplate = new JdbcTemplate();
26 // 注入dataSource
27 jdbcTemplate.setDataSource(dataSource);
28 return jdbcTemplate;
29 }
30
31 /**
32 * 创建事务管理器
33 */
34 @Bean
35 public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {
36 DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
37 transactionManager.setDataSource(dataSource);
38 return transactionManager;
39 }
40
41 }

六、测试

使用xml配置文件实现事务管理时使用 new ClassPathXmlApplicationContext("bean.xml")加载配置文件;

使用完全注解实现事务管理时使用 new AnnotationConfigApplicationContext(TxConfig.class)加载配置类。
1     @Test
2 public void accountTest2() {
3 //完全注解时的测试(加载配置类);xml配置文件测试时使用 new ClassPathXmlApplicationContext("bean.xml")加载配置文件
4 ApplicationContext context = new AnnotationConfigApplicationContext(TxConfig.class);
5 UserService userService = context.getBean("userService", UserService.class);
6 userService.accountMoney();
7 }

Spring实现MySQL事务操作的更多相关文章

  1. 第二百八十六节,MySQL数据库-MySQL事务操作(回滚)

    MySQL数据库-MySQL事务操作(回滚) 事务用于将某些操作的多个SQL作为原子性操作,一旦有某一个出现错误,即可回滚到原来的状态,从而保证数据库数据完整性. 举例:有这样一张表 从表里可以看出张 ...

  2. 学习ThinkPHP的第20天--MySQL事务操作、查询事件、一对一关联

    之所以从20天才开始写随笔记是因为之前没搞自己的博客 从20天开始记录我在ThinkPHP中的点点滴滴 1.MySQL事务操作 /**事务操作*/ //startTrans启动事务.rollback回 ...

  3. Spring中的事务操作

    事务的特性 原子性:强调事务的不可分割. 一致性:事务的执行的前后数据的完整性保持一致. 隔离性:一个事务执行的过程中,不应该受到其他事务的干扰. 持久性:事务一旦结束,数据就持久化到数据库. 如果不 ...

  4. Spring 中的事务操作、注解、以及 XML 配置

    事务 事务全称叫数据库事务,是数据库并发控制时的基本单位,它是一个操作集合,这些操作要么不执行,要么都执行,不可分割.例如我们的转账这个业务,就需要进行数据库事务的处理. 转账中至少会涉及到两条 SQ ...

  5. (转)Spring中的事务操作

    http://blog.csdn.net/yerenyuan_pku/article/details/70024364 事务的回顾 什么是事务 事务是逻辑上的一组操作,组成这组操作的各个逻辑单元,要么 ...

  6. MySQL事务操作

    在 MySQL 命令行的默认设置下,事务都是自动提交的,即执行 SQL 语句后就会马上执行 COMMIT 操作.因此要显式地开启一个事务务须使用命令 BEGIN 或 START TRANSACTION ...

  7. spring mybatis mysql 事务不起作用

    之前框架事务应该是好的,不过这次做了些修改,不知如何竟然不好用了,整理了好半天,java框架配置就是吓人,有一点不熟悉的就可能暴露问题,好处是又深入的了解了配置原理. 开始以为是mysql不支持事务的 ...

  8. spring boot mysql 事务

    mysql默认 事务自动提交.即:每条insert/update/delete语句,不需要程序手工提交事务,而是mysql自行提交了. 如果我们想实现程序事务提交,需要事先关闭mysql的自动提交事务 ...

  9. spring 编程式事务管理和声明式事务管理

    编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...

  10. Spring编程式事务管理及声明式事务管理

    本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...

随机推荐

  1. chatgpt的api联网报错问题解决:openai公司的api联网报错解决

    chatgpt是啥,这里不讲,openai是啥这里也不讲.要知道我们不论是通过网页web使用chatgpt还是使用api方式通过客户端使用chatgpt都是需要使用外国IP的, 为啥我们不能访问ope ...

  2. 强化学习baseline论文—— rainbow算法中给出实验结果的54个atari2600游戏名称列表

    alien amidar assault asterix asteroids atlantis bank_heist battle_zone beam_rider berzerk bowling bo ...

  3. Deep Learning —— 异步优化器 —— RMSpropAsync —— 异步RMSprop

    看到了一个概念,叫做异步更新优化器,也就是使用异步的方式实现deep learning中的参数优化的method,这个概念比较新奇,虽然看到的异步更新神经网络的代码比较多,但是很少见到有人单独把异步优 ...

  4. 人工智能领域在显卡短缺之下的思考——“【亦】微笑面对显卡短缺:不买更快乐”有感——mindspore快快成长

    划水之时看了看B站视频: [亦]微笑面对显卡短缺:不买更快乐 https://www.bilibili.com/video/BV1RZ4y1c7qT 在显卡短缺之下不仅仅电脑游戏玩家被卡脖子就连高校和 ...

  5. css 样式 element.style 覆盖问题

    问题: 我们在写网页定制样式的时候发现展示效果跟我们预想的不一样? 打开F12一看原来是element.style 覆盖的我定义的效果. 解决: 只要在定义的内容后面加上 !important 就行啦 ...

  6. 拈花云科基于 Apache DolphinScheduler 在文旅业态下的实践

    作者|云科NearFar X Lab团队 左益.周志银.洪守伟.陈超.武超 一.导读 无锡拈花云科技服务有限公司(以下简称:拈花云科)是由拈花湾文旅和北京滴普科技共同孵化的文旅目的地数智化服务商.20 ...

  7. Apache SeaTunnel社区首位学生Committer诞生!

    采访对象 | 陈炳烨 采访人&编辑 | Debra Chen Apache SeaTunnel社区第一位学生Committer就此诞生!这位来自西安交通大学软件工程专业的同学从较为简单的文档修 ...

  8. Flex相册

    有一个项目用到了Flex,于是抽时间用flex与java做了一个相册,并且添加了上传功能,不过暂时没有针对具体的用户进行存储.下面是图片:  

  9. CRC算法原理、推导及实现

    CRC, Cyclic Redundancy Check, 循环冗余校验 1. 基本原理 CRC的本质是除法,把待检验的数据当作一个很大(很长)的被除数,两边选定一个除数(有的文献叫poly),最后得 ...

  10. Linux与windows共享文件的神器:samba

    一.什么是samba? 搭建Samba服务器是为了实现Linux共享目录之后,在Windows可以直接访问该共享目录. 现在介绍如何在ubuntu 16.04系统中搭建Samba服务. 二 .samb ...