事务操作步骤:

    <!-- 第一步、配置事务管理器 -->
<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. Vue表单绑定(单选按钮,选择框(单选时,多选时,用 v-for 渲染的动态选项)

    <!DOCTYPE html><html>    <head>        <meta charset="utf-8">      ...

  2. android java.lang.ExceptionInInitializerError

    11-08 13:36:05.108: E/AndroidRuntime(5318): java.lang.ExceptionInInitializerError 11-08 13:36:05.108 ...

  3. jqgrid 对编辑行填写的内容做格式验证

    有时,我们需要在基于jqgrid表格编辑行的单元格做规范验证.jqgrid提供有支持,通过设置字段的editrules属性来约束格式. 约束方式: 1.内置的约束参数 (required: true, ...

  4. Elasticsearch 简介

    1. 背景 Elasticsearch 在公司的使用越来越广,很多同事之前并没有接触过 Elasticsearch,所以,最近在公司准备了一次关于 Elasticsearch 的分享,整理成此文.此文 ...

  5. 20155209林虹宇逆向及Bof基础实验报告

    20155209林虹宇逆向及Bof基础实验报告 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符 ...

  6. Exp9 20155218 Web安全基础实践

    Exp9 Web安全基础实践 1.实验环境配置: 1.在命令行里执行:java -jar webgoat-container-7.1-exec.jar运行WebGoat,文件夹里明明有了,但是没成功: ...

  7. 2017-2018-2 《网络对抗技术》 20155310 第二周 Exp1 PC平台逆向破解(5)M

    2017-2018-2 <网络对抗技术> 20155310 第二周 Exp1 PC平台逆向破解(5)M 一.实践目标 1.1实践介绍 本次实践的对象是一个名为pwn1的linux可执行文件 ...

  8. JavaEE笔记(十一)

    Spring beans使用参数占位符(JDBC配置读取示例) beans.xml配置文件 <?xml version="1.0" encoding="UTF-8& ...

  9. 在Windows商店应用中使用浅色主题

    在开发商店应用时会遇到这样的情况,设计师给我们的设计是浅色背景/深色文本,而商店应用默认是深色背景/浅色文本.那我们需要在每个页面去显式声明背景色和前景色吗,这显然是不理想的.这时就需要设置应用的主题 ...

  10. 【分享】20个非常有用的Java程序片段

    福利来啦!!! 刚看到的一篇好东东,分享给大家,这些代码留着哦,以后会用得着的... 原文地址:http://developer.51cto.com/art/201306/398347.htm 1. ...