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. javascript关于box2djs和matterjs之间的选择

    javascript关于box2djs和matterjs之间的选择box2djs资料少很多时候需要看c++版本资料 然后转化成js 还有转化成像素坐标不准确 matterjs文档丰富 上手容易 建议用 ...

  2. Linux常用命令复习

    1> 查看文件信息:ls ls是英文单词list的简写,其功能为列出目录的内容,是用户最常用的命令之一,它类似于DOS下的dir命令. Linux文件或者目录名称最长可以有265个字符,“.”代 ...

  3. 网页版的支付宝授权登录(vue+java)

    api接口文档:https://docs.open.alipay.com/289/105656 后台管理系统原本是用账号密码登录的,不过需求要改成支付宝授权, 前端仅仅需要改登录页,以及添加一个授权返 ...

  4. nyoj 822-画图 (*)

    822-画图 内存限制:64MB 时间限制:1000ms 特判: No 通过数:133 提交数:187 难度:0 题目描述: 计算机画图也挺有趣的哈!那我们就来用计算机画幅图吧... 输入描述: 输入 ...

  5. nyoj 596-谁是最好的Coder (greater, less)

    596-谁是最好的Coder 内存限制:64MB 时间限制:1000ms 特判: No 通过数:15 提交数:28 难度:0 题目描述: 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的co ...

  6. vue监听移动设备的返回事件

    在公共方法文件common.js中实现一个存储当前历史记录的方法 common.js // 存储当前历史记录点,实现控制手机物理返回键的按钮事件 var pushHistory = function ...

  7. 20191010-7 alpha week 1/2 Scrum立会报告+燃尽图 05

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

  8. IDEA连接Redis

    1.创建一个Maven项目 2.在src下的pom.xml文件里,添加相关包引用 <?xml version="1.0" encoding="UTF-8" ...

  9. 2019-9-11:渗透测试,基础学习,ubuntu搭建LAMP

    一,apache web服务器安装 1,sudo apt-get install apache2 2,systemctl status apache2,检查apache2是否开启 #开启.关闭和重启a ...

  10. 结合开源软件kaptcha讲解登录验证码功能的实现

    一.验证码生成之配置使用kaptcha 使用google开源的验证码实现类库kaptcha,通过maven坐标引入 <dependency> <groupId>com.gith ...