Spring事务实例:

    entity实体类:

public class Accounts {
private int accountid;
private String accountname;
private double balance; public int getAccountid() {
return accountid;
} public void setAccountid(int accountid) {
this.accountid = accountid;
} public String getAccountname() {
return accountname;
} public void setAccountname(String accountname) {
this.accountname = accountname;
} public double getBalance() {
return balance;
} public void setBalance(double balance) {
this.balance = balance;
}
}

    dao层接口:

    //加钱
void addMoney(double money); //减钱
void subMoney(double money);

    daoimpl实现类接口:

    @Override
@Transactional(propagation = Propagation.REQUIRED)
public void addMoney(double money) {
String sql="UPDATE accounts SET balance=balance+? WHERE accountname='张三'";
this.getJdbcTemplate().update(sql,money);
} @Override
@Transactional(propagation = Propagation.REQUIRED)
public void subMoney(double money) {
String sql="UPDATE accounts SET balance=balance-? WHERE accountname='小红'";
this.getJdbcTemplate().update(sql,money);
}

    service业务层:

    //转账
void transferMoney();

    service业务实现层:

    //转账的方法
/*@Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)*/
@Transactional
@Override
public void transferMoney() {
accountDao.subMoney(); //价钱
int num=/; //模拟一个异常 使用事务解决
accountDao.addMoney();
}

    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: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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--添加jdbc-->
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> <!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"></property>
<property name="driverClassName" value="${driver}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean> <!--配置jdbcTemplate核心对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--Dao接口的实现类对象-->
<bean id="accountDao" class="com.te.daoimpl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!--service接口的实现类对象-->
<bean id="accountService" class="com.te.serviceImpl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean> <!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--配置事务-->
   <!--方式一:工厂配置-->
<!--配置Spring事务增强的代理工厂Bean-->
<bean id="transactionProxyFactoryBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<--事务管理器-->
<property name="transactionManager" ref="transactionManager"></property>
<--加载主业务对象-->
<property name="target" ref="accountService"></property>
<--设置事务的隔离级别和传播行为-->
<property name="transactionAttributes">
<props>
<--键值key为具体的方法名value,可以为传播行为或隔离级别-->
<prop key="transferMoney">ISOLATION_READ_UNCOMMITTED,PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
  <!--方式二:AOP配置-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transferMoney" propagation="REQUIRED" isolation="READ_COMMITTED" />
</tx:attributes>
</tx:advice>
<!--定义切面-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.te.service.*.*(..))"></aop:advisor>
</aop:config>   <!--方式三:注解式--> 
  <tx:annotation-driven/>
  <context:component-scan base-package="com.te"></context:component-scan>
</beans>

    测试:

    @Test
public void test03(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("application-del.xml");
AccountService accountService =(AccountService)ctx.getBean("accountService");
//转账
accountService.transferMoney();
}

Spring事物实例的更多相关文章

  1. MyBatis6:MyBatis集成Spring事物管理(下篇)

    前言 前一篇文章<MyBatis5:MyBatis集成Spring事物管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事物的做法,本文的目的是在这个的基 ...

  2. spring事物的传播行为

    1.spring事物的传播行为,主要是用来解决业务层拥有事物的方法,相互调用的问题. 2.声明事物, 在代码执行前,开启事务.代码执行完,提交事务 3.spring并没有提供事务具体的处理,而只是调用 ...

  3. spring得到实例和new一个实例,哪个快?

    spring配置的bean是默认单例,那么在程序中,得到一个实例一定比创建一个实例的速度快,也更加省资源.今天实际测试的时候发现,new 一个对象比spring得到一个对象快多了.后面自己又加了个单例 ...

  4. Spring Security4实例(Java config版)——ajax登录,自定义验证

    本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...

  5. Spring Security4实例(Java config 版) —— Remember-Me

    本文源码请看这里 相关文章: Spring Security4实例(Java config版)--ajax登录,自定义验证 Spring Security提供了两种remember-me的实现,一种是 ...

  6. Spring 事物Transaction

    日常开发中Spring 为我们提供了两种事物的定义方式 XML 配置 方式 :这种方式配置起来比较麻烦,但后期比较好进行维护 注解方式:配置起来比较方便,也是日常开发常用的: 我们这里进行第二种注解的 ...

  7. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  8. Spring事物管理--相关要点及配置事物管理器

    事务的四大特征 1.原子性:一个事务中所有对数据库的操作是一个不可分割的操作序列,要么全做要么全不做 2.一致性:数据不会因为事务的执行而遭到破坏 3.隔离性:一个事物的执行,不受其他事务的干扰,即并 ...

  9. spring事物深入了解

    1.问题 1.以前对事物的了解只是停留在声明式事物,配置xml,或使用注解,事物的传播行为也只用过REQUIRED和SUPPORTS,可以说对事物的了解很模糊. 2.直到在开发中遇到问题.. 问题的描 ...

随机推荐

  1. Apache httpd 2.4.27开启GZIP压缩功能

    转载自素文宅博客:https://blog.yoodb.com/yoodb/article/detail/1373 HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的文件压缩算法,现在的应 ...

  2. sql性能分析(explain关键字)

    explain关键字结果 列名所代表的的含义: Id:MySQL QueryOptimizer 选定的执行计划中查询的序列号.表示查询中执行 select 子句或操作表的顺序,id 值越大优先级越高, ...

  3. 深入理解计算机系统 第八章 异常控制流 Part2 第二遍

    第二遍读这本书,每周花两到三小时时间,能读多少读多少(这次看了第 508~530 页,共 23 页) 第一遍对应笔记链接 https://www.cnblogs.com/stone94/p/10206 ...

  4. MyBatis批量更新动态sql

    <update id="updateDataKetState"> update ${tablespace}.IDEA_DATAKEY_STATE <trim pr ...

  5. 【Linux】Debian 下安装 Apache,MySQL,PHP

    首先,对你的源进行更新: $ sudo apt-get update 第一步--安装 Apache Apache 是一个开源软件,它目前运行在全球超过 50% 的服务器上,是 LAMP(Linux,A ...

  6. PHP 性能优化 - php.ini 配置

    内存 默认设置 memory_limit = 128M 单个进程可使用的内存最大值,这个值的设定可以从以下几点考虑: 应用的类型.如果是内存集中型应用,可增加该值: 单个 PHP 进程平均消耗的内存, ...

  7. thinkphp 获取前端传递过来的参数

    thinkphp 获取前端传递过来的参数 use think\facade\Request; // 获取当前请求的name变量 Request::param('name'); // 获取当前请求的所有 ...

  8. bash:字符串变量查找

    提供了替换文本的查找替换功能,如 sed s/Wintel/Linux/g data (将Wintel替换为Linux)  大命令 下边是基于变量的小命令: 1)查找与替换 ${data/Wintel ...

  9. 🙈羞,Spring Bean 初始化/销毁竟然有这么多姿势

    文章来源:http://1t.click/bfHN 一.前言 日常开发过程有时需要在应用启动之后加载某些资源,或者在应用关闭之前释放资源.Spring 框架提供相关功能,围绕 Spring Bean ...

  10. 读懂JWT的使用,你就会用PHP如何实现了

    要如何用php实现JWT认证,那我们首先就来认识一下什么是JWT.什么是JWTJWT(json web token)是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准.JWT的声明一般被 ...