事务操作步骤:

    <!-- 第一步、配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 第二步、开启事务的注解 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!-- 第三步、在事务类上添加注解
@Transactional
public class UserSerivce { -->

举例如下:

UserDao.java

 package helloworld.txZhuJie;

 import org.springframework.jdbc.core.JdbcTemplate;

 public class UserDao {

     private JdbcTemplate jdbcTemplate;

     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} // 减少
public void reduce(String name,int num) {
// 调用jdbcTemplate对象中的方法实现操作
String sql = "update salary set salary = (salary - ?) where name= ?";
// 表结构:name(varchar 20),salary(int 20)
int rows = jdbcTemplate.update(sql, num, name);
System.out.println("修改行数:" + rows);
} // 增加
public void increase(String name,int num) {
// 调用jdbcTemplate对象中的方法实现操作
String sql = "update salary set salary = (salary + ?) where name= ?";
// 表结构:name(varchar 20),salary(int 20)
int rows = jdbcTemplate.update(sql, num, name);
System.out.println("修改行数:" + rows);
} // 实现添加操作
public void add() {
// 调用jdbcTemplate对象中的方法实现操作
String sql = "insert into salary value(?,?)";
// 表结构:name(varchar 20),salary(int 20)
int rows = jdbcTemplate.update(sql, "Tom", 10000);
System.out.println("插入行数:" + rows);
rows = jdbcTemplate.update(sql, "Jerry", 10000);
System.out.println("插入行数:" + rows);
} }

UserSerivce.java

 package helloworld.txZhuJie;

 import org.springframework.transaction.annotation.Transactional;

 //事务操作注解
@Transactional
public class UserSerivce {
private UserDao userDao; public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void add(){
userDao.add();
} // 工资调整
public void updateAccount(){ userDao.reduce("Tom",1000); // 制造异常
// int n = 100/0; userDao.increase("Jerry",1000); } }

TestTXZhuJie.java

 package helloworld.txZhuJie;

 import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /*
* 事务操作(注解)举例
* 1、配置事务管理器
* 2、配置事务注解
* 3、在使用事务的方法所在类上添加注解
* */
public class TestTXZhuJie {
@Before
public void beforeRun() {
System.out.println("beforeRun");
} // 插入数据
@Ignore
@Test
public void add() {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans_tx_zhujie.xml");
UserSerivce userSerivce = (UserSerivce) context.getBean("userSerivce");
userSerivce.add();
} @Test
public void update() {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans_tx_zhujie.xml");
UserSerivce userSerivce = (UserSerivce) context.getBean("userSerivce");
userSerivce.updateAccount();
} }

beans_tx_zhujie.xml

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:contexnt="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.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">
<!--事务配置文件(注解)--> <!--配置c3p0连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--注入属性-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://192.168.184.130:3306/gxrdb"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean> <!-- 第一步、配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 第二步、开启事务的注解 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!-- 第三步、在事务类上添加注解
@Transactional
public class UserSerivce { --> <!--创建service对象,注入dao对象-->
<bean id="userSerivce" class="helloworld.txZhuJie.UserSerivce">
<property name="userDao" ref="userDao"></property>
</bean> <!--创建DAO对象,注入JdbcTemplate对象-->
<bean id="userDao" class="helloworld.txZhuJie.UserDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!--创建JdbcTemplate对象,注入连接池dataSource-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--引入其它配置文件-->
<!--<import resource="classpath:helloworld/zhuru/beans.xml"/>--> </beans>

Spring之事务操作(注解)的更多相关文章

  1. spring的事务操作(重点)

    这篇文章一起来回顾复习下spring的事务操作.事务是spring的重点, 也是面试的必问知识点之一. 说来这次面试期间,也问到了我,由于平时用到的比较少,也没有关注过这一块的东西,所以回答的不是特别 ...

  2. spring的事务操作

    我们项目一期已经差不多结束了,所以一些细节也被拿了出来,出现最多的就是事务的操作了.因为自己负责的是一个模块(因为是另外一个项目的负责人),所以组员经常会遇到事务的问题,会出现很多奇葩的用法,各种乱用 ...

  3. 对于spring中事务@Transactional注解的理解

    现在spring的配置都喜欢用注解,这边就说下@Transactional 一.如何开启@Transactional支持 要使用@Transactional,spring的配置文件applicatio ...

  4. Spring3:spring的事务操作

    三.事务操作 1.导包 2. jdbc模板与开源连接池(DBCP与C3P0) 2.1DBCP 2.2C3P0 :: 2.3.抽取配置到属性文件   定义一个属性文件  在Spring的配置文件中引入属 ...

  5. Spring初学之spring的事务管理注解

    spring的事务管理,本文的例子是:比如你需要网购一本书,卖书的那一方有库存量以及书的价格,你有账户余额.回想我们在编程中要实现买书这样的功能,由于你的账户表和书的库存量表肯定不是同一张数据库表,所 ...

  6. Spring之事务操作(配置文件)

    UserDao.java package helloworld.tx; import org.springframework.jdbc.core.JdbcTemplate; public class ...

  7. Spring中的事务操作

    事务的特性 原子性:强调事务的不可分割. 一致性:事务的执行的前后数据的完整性保持一致. 隔离性:一个事务执行的过程中,不应该受到其他事务的干扰. 持久性:事务一旦结束,数据就持久化到数据库. 如果不 ...

  8. (转)Spring中的事务操作

    http://blog.csdn.net/yerenyuan_pku/article/details/70024364 事务的回顾 什么是事务 事务是逻辑上的一组操作,组成这组操作的各个逻辑单元,要么 ...

  9. Spring 中的事务操作、注解、以及 XML 配置

    事务 事务全称叫数据库事务,是数据库并发控制时的基本单位,它是一个操作集合,这些操作要么不执行,要么都执行,不可分割.例如我们的转账这个业务,就需要进行数据库事务的处理. 转账中至少会涉及到两条 SQ ...

随机推荐

  1. 在nodeJS中操作文件系统(二)

    在nodeJS中操作文件系统(二)   1. 移动文件或目录 在fs模块中,可以使用rename方法移动文件或目录,使用方法如下:     fs.rename(oldPath,newPath,call ...

  2. Python2.7-filecmp

    filecmp 模块,定义了比较文件或目录的函数,比较文件只会有 True 和 False 两种结果,比较目录会返回目录下相同的文件,不同的文件,出错的文件.比较文件也可以用 difflib 模块,d ...

  3. Java学习笔记--Java开发坏境搭建

    一.安装JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 根据自己系统选择 ...

  4. day35

    今日内容: 1.进程间互相通信(IPC机制) 2.生产者消费者模型 3.线程理论 4.线程开启的两种方式 5.线程相关属性方法 6.守护线程 7.线程互斥锁 1.进程间相互通信(IPC机制) 主要是一 ...

  5. 带你看懂大数据采集引擎之Flume&采集目录中的日志

    一.Flume的介绍: Flume由Cloudera公司开发,是一种提供高可用.高可靠.分布式海量日志采集.聚合和传输的系统,Flume支持在日志系统中定制各类数据发送方,用于采集数据:同时,flum ...

  6. Spring + SpringMVC配置

    代码结构如下 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xs ...

  7. 20155313 杨瀚 《网络对抗技术》实验九 Web安全基础

    20155313 杨瀚 <网络对抗技术>实验九 Web安全基础 一.实验目的 本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 二.基础问题回答 1.SQL注入攻 ...

  8. 浅谈android Service和BroadCastReceiver

    1.题记 Android中的服务和windows中的服务是类似的东西,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序. 广播接收者(BroadcastRece ...

  9. 初级字典树查找在 Emoji、关键字检索上的运用 Part-3

    系列索引 Unicode 与 Emoji 字典树 TrieTree 与性能测试 生产实践 生产实践 我们最终要解决 Emoji 在浏览器和打印物上的显示一致. 进行了多番对比,,在显示效果和精度上,m ...

  10. 前端项目模块化的实践2:使用 Webpack 打包基础设施代码

    以下是关于前端项目模块化的实践,包含以下内容: 搭建 NPM 私有仓库管理源码及依赖: 使用 Webpack 打包基础设施代码: 使用 TypeScript 编写可靠类库 使用 TypeScript ...