一.xml配置文件形式
通过转账案例,学习事务管理
1.建立数据库
2.编写entity

  1 package huguangqin.com.cnblogs.entity;
  2
  3 public class User {
  4      private Integer id;
  5      private String name;
  6      private Double money;
  7
  8     public User() {
  9          super();
 10      }
 11
 12     public User(Integer id, String name, Double money) {
 13          super();
 14          this.id = id;
 15          this.name = name;
 16          this.money = money;
 17      }
 18
 19     public Integer getId() {
 20          return id;
 21      }
 22
 23     public void setId(Integer id) {
 24          this.id = id;
 25      }
 26
 27     public String getName() {
 28          return name;
 29      }
 30
 31     public void setName(String name) {
 32          this.name = name;
 33      }
 34
 35     public Double getMoney() {
 36          return money;
 37      }
 38
 39     public void setMoney(Double money) {
 40          this.money = money;
 41      }
 42
 43 }
 44 

3.编写dao

  1 package huguangqin.com.cnblogs.dao;
  2
  3 public interface UserDao {
  4      void increasement(int id, double money);
  5
  6     void decreasement(int id, double money);
  7  }
  8 

4.编写daoImpl

  1 package huguangqin.com.cnblogs.daoImpl;
  2
  3 import org.springframework.jdbc.core.support.JdbcDaoSupport;
  4
  5 import huguangqin.com.cnblogs.dao.UserDao;
  6
  7 public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
  8
  9     @Override
 10      public void increasement(int id, double money) {
 11          String sql = "update t_bank set money=money+? where id = ?";
 12          getJdbcTemplate().update(sql, money, id);
 13      }
 14
 15     @Override
 16      public void decreasement(int id, double money) {
 17          String sql = "update t_bank set money=money-? where id = ?";
 18          getJdbcTemplate().update(sql, money, id);
 19      }
 20
 21 }
 22 

5.编写service

  1 package huguangqin.com.cnblogs.service;
  2
  3 public interface IUserService {
  4      void tranfer(int where, int to, double money);
  5  }
  6 

6.编写serviceImpl

  1 package huguangqin.com.cnblogs.serviceImpl;
  2
  3 import org.springframework.beans.factory.annotation.Autowired;
  4
  5 import huguangqin.com.cnblogs.dao.UserDao;
  6  import huguangqin.com.cnblogs.service.IUserService;
  7
  8 public class UserServiceImpl implements IUserService {
  9      //调用UserDao操作数据库,spring下调用接口,并注入实例对象
 10     @Autowired
 11      private UserDao ud;
 12
 13     public void setUd(UserDao ud) {
 14          this.ud = ud;
 15      }
 16
 17     @Override
 18      public void tranfer(int where, int to, double money) {
 19         ud.decreasement(where, money);
 20          ud.increasement(to, money);
 21      }
 22  }
 23 

7.编写db.properties

  1 jdbc.driverClass=com.mysql.jdbc.Driver
  2 jdbc.url=jdbc:mysql:///spring_day02
  3 jdbc.user=root
  4 jdbc.password=root

8.编写applicationContext.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2
  3 <beans
  4  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5  xmlns="http://www.springframework.org/schema/beans"
  6  xmlns:context="http://www.springframework.org/schema/context"
  7  xmlns:aop="http://www.springframework.org/schema/aop"
  8  xmlns:tx="http://www.springframework.org/schema/tx"
  9  xsi:schemaLocation="http://www.springframework.org/schema/beans
 10                      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 11                      http://www.springframework.org/schema/context
 12                      http://www.springframework.org/schema/context/spring-context-4.2.xsd
 13                      http://www.springframework.org/schema/aop
 14                      http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
 15                      http://www.springframework.org/schema/tx
 16                      http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
 17
 18  <!--读取db.propertiey  -->
 19  <context:property-placeholder location="classpath:db.properties"/>
 20
 21 <!-- 配置连接池到spring容器 -->
 22  <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 23      <property name="driverClass" value="${jdbc.driverClass}"></property>
 24      <property name="jdbcUrl" value="${jdbc.url}"></property>
 25      <property name="user" value="${jdbc.user}"></property>
 26      <property name="password" value="${jdbc.password}"></property>
 27  </bean>
 28
 29 <!-- 配置核心事务管理器 -->
 30  <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 31      <property name="dataSource" ref="dataSource"></property>
 32  </bean>
 33
 34 <!-- 配置事务管理通知 -->
 35  <tx:advice id="txAdvice" transaction-manager="transactionManager">
 36      <tx:attributes>
 37          <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
 38          <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
 39          <tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
 40      </tx:attributes>
 41  </tx:advice>
 42
 43 <!-- 配置织入 -->
 44  <aop:config>
 45      <aop:pointcut expression="execution(* huguangqin.com.cnblogs.serviceImpl.*ServiceImpl.*(..))" id="txPc"/>
 46  </aop:config>
 47
 48 <!-- 配置dao -->
 49  <bean name="userDao" class="huguangqin.com.cnblogs.daoImpl.UserDaoImpl">
 50      <property name="dataSource" ref="dataSource"></property>
 51  </bean>
 52
 53 <!-- 配置service -->
 54  <bean name="userService" class="huguangqin.com.cnblogs.serviceImpl.UserServiceImpl">
 55      <property name="ud" ref="userDao"></property>
 56  </bean>
 57  </beans>
 58 

二.Annotation形式
与xml相比需修改以下文件
1.applicationContext.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2
  3 <beans
  4  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5  xmlns="http://www.springframework.org/schema/beans"
  6  xmlns:context="http://www.springframework.org/schema/context"
  7  xmlns:aop="http://www.springframework.org/schema/aop"
  8  xmlns:tx="http://www.springframework.org/schema/tx"
  9  xsi:schemaLocation="http://www.springframework.org/schema/beans
 10                      http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
 11                     http://www.springframework.org/schema/context
 12                      http://www.springframework.org/schema/context/spring-context-4.2.xsd
 13                      http://www.springframework.org/schema/aop
 14                      http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
 15                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
 16
 17  <!--读取db.propertiey  -->
 18  <context:property-placeholder location="classpath:db.properties"/>
 19
 20 <!-- 配置连接池到spring容器 -->
 21  <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 22      <property name="driverClass" value="${jdbc.driverClass}"></property>
 23      <property name="jdbcUrl" value="${jdbc.url}"></property>
 24      <property name="user" value="${jdbc.user}"></property>
 25      <property name="password" value="${jdbc.password}"></property>
 26 </bean>
 27
 28 <!-- 配置核心事务管理器 -->
 29  <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 30      <property name="dataSource" ref="dataSource"></property>
 31  </bean>
 32
 33 <!--开启注解管理事务开关  -->
 34  <tx:annotation-driven transaction-manager="transactionManager"/>
 35
 36 <!-- 配置dao -->
 37  <bean name="userDao" class="huguangqin.com.cnblogs.daoImpl.UserDaoImpl">
 38      <property name="dataSource" ref="dataSource"></property>
 39  </bean>
 40
 41 <!-- 配置service -->
 42  <bean name="userService" class="huguangqin.com.cnblogs.serviceImpl.UserServiceImpl">
 43      <property name="ud" ref="userDao"></property>
 44  </bean>
 45  </beans>
 46 

2.serviceImpl类

  1 package huguangqin.com.cnblogs.serviceImpl;
  2
  3 import org.springframework.beans.factory.annotation.Autowired;
  4  import org.springframework.transaction.annotation.Transactional;
  5
  6 import huguangqin.com.cnblogs.dao.UserDao;
  7  import huguangqin.com.cnblogs.service.IUserService;
  8
  9 @Transactional
 10  public class UserServiceImpl implements IUserService {
 11      @Autowired
 12      private UserDao ud;
 13
 14     public UserDao getUd() {
 15          return ud;
 16      }
 17
 18     public void setUd(UserDao ud) {
 19          this.ud = ud;
 20      }
 21
 22     @Override
 23      // @Transactional(isolation = Isolation.DEFAULT, propagation =Propagation.REQUIRED, readOnly = false)
 24      public void tranfer(int where, int to, double money) {
 25          ud.decreasement(where, money);
 26          ud.increasement(to, money);
 27      }
 28
 29 }
 30 

转载于:https://www.cnblogs.com/huguangqin/p/7488614.html

spring笔记4-事务管理相关推荐

  1. 【Spring学习笔记 九】Spring声明式事务管理实现机制

    什么是事务?事务就是把一系列的动作当成一个独立的工作单元,这些动作要么全部完成,要么全部不起作用,关乎数据准确性的地方我们一定要用到事务,防止业务逻辑出错. 什么是事务管理,事务管理对于企业应用而言至 ...

  2. Spring中的事务管理详解

    在这里主要介绍Spring对事务管理的一些理论知识,实战方面参考上一篇博文: http://www.cnblogs.com/longshiyVip/p/5061547.html 1. 事务简介: 事务 ...

  3. (转)使用Spring配置文件实现事务管理

    http://blog.csdn.net/yerenyuan_pku/article/details/52886207 前面我们讲解了使用Spring注解方式来管理事务,现在我们就来学习使用Sprin ...

  4. Spring配置文件详解三:Spring声明式事务管理

    1.声明式事务管理 Spring提供了声明式事务管理,这是通过Spring AOP实现的. 原理:Spring中进行事务管理的通常方式是利用AOP(面向切片编程)的方式,为普通java类封装事务控制, ...

  5. Spring JDBC-Spring对事务管理的支持

    概述 事务管理关键抽象 Spring事务管理的实现类 Spring JDBC 和MybBatis的事务管理器的配置 JPA的事务管理器的配置 Hibernate的事务管理器的配置 JTA 的事务管理器 ...

  6. Spring框架的事务管理及应用

    Spring框架简介 Spring框架是一个2003年2月才出现的开源项目,该开源项目起源自Rod Johnson在2002年末出版的<Expert One-on-One J2EE Design ...

  7. Spring(四)——AOP、Spring实现AOP、Spring整合Mybatis、Spring中的事务管理

    文章目录 1. 什么是AOP 2. 使用Spring实现AOP 2.1 使用Spring的API 接口实现 2.2 自定义实现 2.3 使用注解实现 3. 整合MyBatis 3.1 MyBatis- ...

  8. Spring入门5.事务管理机制

    Spring入门5.事务管理机制 20131126 代码下载 : 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 回顾之前的知识,Spring 最为核心的两个部分 ...

  9. spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

    1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...

  10. Spring声明式事务管理、事务的传播行为xml配置

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1. <tx:method name="insert*" propagat ...

最新文章

  1. C++ Map Source
  2. 关闭服务器 找不到文件,在服务器上找不到文件时阻止代码崩溃的方法?
  3. Pix2Pix Z图像翻译系统(自制)
  4. 改变bantchsize发现loss增大_肺内发现磨玻璃结节,一定就是早期肺癌吗?
  5. KVM 虚拟化技术以及 KVM 和云计算的关系
  6. vba mysql 没有为命令对象设置命令_怎样解决“没有为命令对象设置命令”的错误? - .Net论坛 - 51CTO技术论坛_中国领先的IT技术社区...
  7. 基于Spring MVC的Excel文件上传
  8. 深入理解JavaScript (5) —— 闭包
  9. caffe---测试模型分类结果并输出(python )
  10. 将.sql文件导入数据库
  11. cad小插件文字刷_必备CAD插件大全,内含最全字体库
  12. MATLAB将界面语言由中文改成英文
  13. java开发steam平台_stma(steam平台)
  14. 该如何去认知Level 2 十档行情数据?
  15. Learning AV Foundation(二)AVAudioPlayer
  16. Web——HTML常见标签及用法
  17. 让图片说出声音来(利用讯飞API实现图片转文字和文字转语音)
  18. 使用Linux命令删除Android的一些垃圾文件
  19. 对DHCP客户端创建黑名单或白名单
  20. 非常实用FPGA实现CRC校验介绍和代码生成工具

热门文章

  1. 轻量化版本优于MobileNet系列 | Tokens-to-Token ViT: Training Vision Transformers from Scratch on ImageNet
  2. php的$_server例子,php全局变量$_SERVER的四个例子
  3. 人工智能 对比试验_人工智能与药物研发
  4. python计算复制比_vbs实现只复制比目标文件更新的文件
  5. 信安教程第二版-第16章网络安全风险评估技术原理与应用
  6. qweb加html文件,将本地html文件加载到Pyside QwebVi中
  7. linux安装python和pip3,Linux安装python3.6 和pip
  8. c语言一级考试题目第四季度,模拟试题8套
  9. 好用的python学习软件_5种好用的Python工具!Python学习分析
  10. Appium基础:Desired Capabilities详讲