Spring事务管理transactionManager
bean.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property ref="dataSource" name="dataSource"/>
</bean>
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!--mysql-->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--service-->
<bean class="com.war.service.Service" id="service">
<property name="dao" ref="dao"></property>
</bean>
<!--dao-->
<bean class="com.war.dao.DaoImpl" id="dao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--jdbcTemplate-->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
DaoImpl.java
package com.war.dao;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Created by Administrator on 2017/6/21.
*/
public class DaoImpl implements Dao {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public String addMoney() {
String sql = "update account set money =money + ? where name = ?";
jdbcTemplate.update(sql,1000,"tom");
System.out.println("add over");
return null;
}
@Override
public String reduceMoney() {
String sql = "update account set money = money - ? where name = ?";
jdbcTemplate.update(sql,1000,"tim");
System.out.println("reduce over");
return null;
}
}
Service.java
package com.war.service;
import com.war.dao.DaoImpl;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by Administrator on 2017/6/21.
*/
@Transactional
public class Service {
private DaoImpl dao;
public DaoImpl getDao() {
return dao;
}
public void setDao(DaoImpl dao) {
this.dao = dao;
}
public void TransferAccounts(){
dao.reduceMoney();
// int a = 10/0;
dao.addMoney();
System.out.println("over");
}
}
TestTransaction.java
package com.war;
import com.war.service.Service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2017/6/21.
*/
public class TestTransaction {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
Service service = (Service) ac.getBean("service");
service.TransferAccounts();
}
}
Spring事务管理transactionManager的更多相关文章
- 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】
一.JDBC编程特点 静态代码+动态变量=JDBC编程. 静态代码:比如所有的数据库连接池 都实现了DataSource接口,都实现了Connection接口. 动态变量:用户名.密码.连接的数据库. ...
- spring事务管理器设计思想(二)
上文见<spring事务管理器设计思想(一)> 对于第二个问题,涉及到事务的传播级别,定义如下: PROPAGATION_REQUIRED-- 如果当前没有事务,就新建一个事务.这是最常见 ...
- 事务管理(下) 配置spring事务管理的几种方式(声明式事务)
配置spring事务管理的几种方式(声明式事务) 概要: Spring对编程式事务的支持与EJB有很大的区别.不像EJB和Java事务API(Java Transaction API, JTA)耦合在 ...
- Spring事务管理器的应对
Spring抽象的DAO体系兼容多种数据访问技术,它们各有特色,各有千秋.像Hibernate是非常优秀的ORM实现方案,但对底层SQL的控制不太方便:而iBatis则通过模板化技术让你方便地控制SQ ...
- Spring事务管理(转)
1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱:然后ATM出1000元钱.这两个步骤必须是 ...
- [Spring框架]Spring 事务管理基础入门总结.
前言:在之前的博客中已经说过了数据库的事务, 不过那里面更多的是说明事务的一些锁机制, 今天来说一下Spring管理事务的一些基础知识. 之前的文章: [数据库事务与锁]详解一: 彻底理解数据库事务一 ...
- Spring 事务管理高级应用难点剖析--转
第 1 部分 http://www.ibm.com/search/csass/search/?q=%E4%BA%8B%E5%8A%A1&sn=dw&lang=zh&cc=CN& ...
- MyBatis6:MyBatis集成Spring事务管理(下篇)
前言 前一篇文章<MyBatis5:MyBatis集成Spring事务管理(上篇)>复习了MyBatis的基本使用以及使用Spring管理MyBatis的事务的做法,本文的目的是在这个的基 ...
- 【转】Spring事务管理
原文链接 在 Spring 中,事务是通过 TransactionDefinition 接口来定义的.该接口包含与事务属性有关的方法.具体如清单 1 所示: 清单 1. TransactionDefi ...
随机推荐
- C博客作业01——分支、顺序结构
1.本章学习总结 1.1思维导图 本章学习体会及代码量学习体会 1.2.1学习体会 在暑假的时候就有加入新生学习群,对C语言有一定的基础,所以这周的学习相对轻松,但一些细节方面的知识并不是很了解.在这 ...
- python_内置函数
#内置函数 #1.abs 获取绝对值 # abs(-10) # --->10 # # abs(10) # --->10 # # abs(0) # --->0 #2.all() 参数为 ...
- leetcode 78,236,300
---恢复内容开始--- 2018.3.16目前已刷27题,打卡记录有意思的题目. leetcode78 subsets 思路1:DFS遍历子集,每遇到一个数就把该数加上原来的子集变成新的子集. cl ...
- 欧姆龙PLC CP1E型号的90,91,190
I/O存储区的CIO区,输入位CIO0-CIO99 对于NA型,模拟输入0和1将分别占用CIO90和CIO91 输出位CIO100-CIO199,对于NA型,模拟输出0将占用CIO190
- Element.scrollIntoView()
Element.scrollIntoView() 方法让当前的元素滚动到浏览器窗口的可视区域内 语法 element.scrollIntoView(); element.scrollIntoView( ...
- web plugins
<build> <resources> <resource> <directory>src/main/java</directory> &l ...
- js 如何判断数组元素是否存在重复项
1.如何判断数组元素是否存在重复项 1)定义测试数组 //定义测试的数组(1个没有重复元素,1个有重复元素) var arr1 = new Array("111","33 ...
- IDEA主类文件需要放置在SRC文件下,非包内
构建flash项目后,主类文件需要放置在src下,而不是在某个包内. 这样才会找到入口主类,然后有输出. 主类里面有引用其他类,需要使用 import * 全部引入.
- centes7安装wdcp
CentOS7安装WDCP3 CentOS7安装WDCP3.2面板教程 到此WDCP安装完毕
- php 面向对象二
多态: 多态就是多种形态:多态分为方法重写和方法重载,但是php不支持方法重载 重写: 子类和父类的方法名必须一致,严格标准要求参数必须一致,但是参数可以不一致 子类中覆盖的方法不能比父类的方法访问权 ...