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. 易初大数据 2019年11月10日 spss习题 王庆超

    ◆1.一个数据文件包含下列数据,5个家庭没有汽车(编码为0),20个家庭有一辆汽车(编码唯1),10个家庭拥有两辆汽车(编码为2)指出下列哪种统计量适用于描述该数据并计算出统计量的值.A A拥有汽车数 ...

  2. thinkphp分页样式css代码

    <style type="text/css"> .Pagination a:hover,.current{background-color: #f54281;borde ...

  3. 技术人如何利用 github+Jekyll ,搭建一个独立免费的技术博客

    上次有人留言说,技术博客是程序员的标配,但据我所知绝大部分技术同学到现在仍然没有自己的技术博客.原因有很多,有的是懒的写,有的是怕写不好,还有的是一直想憋个大招,幻想做到完美再发出来,结果一直胎死腹中 ...

  4. Java代码自动生成,生成前端vue+后端controller、service、dao代码,根据表名自动生成增删改查功能

    本项目地址:https://github.com/OceanBBBBbb/ocean-code-generator 项目简介 ocean-code-generator采用(适用):     ,并使用m ...

  5. VLAN实验(1)Access接口

    1.选择两台S3700的交换机,5台PC机,并按照下图链接好并填写IP,完成此拓扑图 2.由于现在我们还没有划分VLAN,这5台PC,还在同一个VLAN中,现在我们启动所有的设备,这是所有的主机应该是 ...

  6. JPA中使用@Query注解多表联查

    原生SQL: select `user`.id, `user`.`name`,dept.name deptName,sum(sd.score) SumScore from `user` LEFT JO ...

  7. Blocked a frame with origin XXX from accessing a cross-origin 。iframe跨域问题

    在前端开发的过程中,我们常常会用到iframe去在我们的页面中引用一个子页面,而父子页面又常常会有交互.在同域情况下,子页面如果想要访问父页面中的window对象中的方法的话,直接在当前页面中使用wi ...

  8. vue—自定义指令

    今日分享—自定义指令 需要学习的点: modifiers属性的具体实例就是v-on:click.stop=”handClick” 一样,为指令添加一个修饰符. 全局指令:新建一个newDir.js i ...

  9. Django使用mysql数据的流程

    创建一个mysql数据库 1.打开终端(cmd): 输入: mysql -uroot -p 密码:*** 输入: create database 数据库名字; 2.在settings中进行配置 DAT ...

  10. 20191010-5 alpha week 1/2 Scrum立会报告+燃尽图 03

    此作业要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/8748] 一.小组情况 队名:扛把子 组长:迟俊文 组员:宋晓丽 梁梦瑶 ...