UserDao.java

 package helloworld.tx;

 import org.springframework.jdbc.core.JdbcTemplate;

 public class UserDao {

     private JdbcTemplate jdbcTemplate;

     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} // 实现添加操作
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);
} // 减少
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);
} }

UserSerivce.java

 package helloworld.tx;

 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); } }
TestTX.java
 package helloworld.tx;

 import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /*
* 事务操作举例
* */
public class TestTX {
@Before
public void beforeRun() {
System.out.println("beforeRun");
} // 插入数据
@Ignore
@Test
public void add() {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans_tx.xml");
UserSerivce userSerivce = (UserSerivce) context.getBean("userSerivce");
userSerivce.add();
} @Test
public void update() {
ApplicationContext context =
new ClassPathXmlApplicationContext("beans_tx.xml");
UserSerivce userSerivce = (UserSerivce) context.getBean("userSerivce");
userSerivce.updateAccount();
} }

beans_tx.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:advice id="txAdvice" transaction-manager="transactionManager">
<!--做事务操作-->
<tx:attributes>
<!--事务操作的方法的匹配规则-->
<!--name:方法名;propagation:隔离级别-->
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="add"/>
</tx:attributes>
</tx:advice>
<!-- 第三步、配置切面-->
<aop:config>
<!--切入点-->
<!-- *表示任意方法;..表示任意参数-->
<aop:pointcut id="pointcut1" expression="execution(* helloworld.tx.UserSerivce.*(..))"></aop:pointcut>
<!--切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"></aop:advisor>
</aop:config> <!--创建service对象,注入dao对象-->
<bean id="userSerivce" class="helloworld.tx.UserSerivce">
<property name="userDao" ref="userDao"></property>
</bean> <!--创建DAO对象,注入JdbcTemplate对象-->
<bean id="userDao" class="helloworld.tx.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> </beans>

Spring之事务操作(配置文件)的更多相关文章

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

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

  2. Spring3:spring的事务操作

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

  3. spring的事务操作

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

  4. Spring之事务操作(注解)

    事务操作步骤: <!-- 第一步.配置事务管理器 --> <bean id="transactionManager" class="org.spring ...

  5. Spring中的事务操作

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

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

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

  7. Spring Framework 中启动 Redis 事务操作

    背景: 项目中遇到有一系列对Redis的操作,并需要保持事务处理. 环境: Spring version 4.1.8.RELEASE Redis Server 2.6.12 (64位) spring- ...

  8. Spring事务操作介绍

    Spring的特色之一,简单而强大的事务管理功能,包括编程式事务和声明式事务. 1. Spring中涉及到事务管理的API有100多个,核心的只有三个: TransactionDefinition.P ...

  9. spring学习(三) ———— spring事务操作

    前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...

随机推荐

  1. OpenCV——LBP(Local Binary Patterns)特征检测

    #include <opencv2/opencv.hpp> #include <iostream> #include "math.h" using name ...

  2. Python2.7-fileinput

    fileinput 模块,对输入的文件流进行迭代操作,可以说是对 open() 的一个扩展,它可以直接修改文件,也可以对他们进行备份 模块方法: fileinput.input([files[, in ...

  3. Win7系统的任务管理器显示不全

    Win7系统的任务管理器显示不全:如下图所示 首先要说明的是:出现这种现象是正常的,因为这种样式是任务管理器的精简模式. 还原方法:双击任务管理器四周的空白边框,即可恢复初始模式. 效果图如下:

  4. Android调用系统的打电话和发短信界面(1.将消息内容带过去2.实现群发)

    package com.example.myapi.sms; import android.app.Activity; import android.content.Intent; import an ...

  5. stop-hbase.sh一直处于等待状态

    今天关闭HBase时,输入stop-hbase.sh一直处于等待状态 解决方法: 先输入:hbase-daemon.sh stop master 再输入:stop-hbase.sh就可以关闭HBase ...

  6. [HAOI2008]排名系统 & [Zjoi2006]GameZ游戏排名系统 BZOJ1862&BZOJ1056

    分析: 平衡树裸题,(学完LCT感觉自己不会普通的Splay了...),维护每个节点的权值大小顺序,和时间戳顺序,之后map维护一下是否存在过,(懒得写字符串hash了). 附上代码: #includ ...

  7. 全虚拟化和半虚拟化的区别 cpu的ring0 ring1又是什么概念? - 转

    http://www.cnblogs.com/xusongwei/archive/2012/07/30/2615592.html ring0是指CPU的运行级别,ring0是最高级别,ring1次之, ...

  8. [2016北京集训试题15]cot-[分块]

    Description Solution 如图,假如我们知道了以任何一个点为顶点的135-180度的前缀和和90-180度的前缀和,我们就可以搞出三角形的面积. 差分.add[i][j]和dev[i] ...

  9. stm32的PWM占空比

    PWM一共有两种模式,PWM1模式:CNT<CRRx为有效电平.CNT>CRRx为无效电平.PWM2模式相反. 有限电平通过设置极性来确定: TIM_OCInitStructure.TIM ...

  10. 28 个 C/C++ 开源 JSON 程序库性能及标准符合程度评测

    28 个 C/C++ 开源 JSON 程序库性能及标准符合程度评测 坊间有非常多的 C/C++ JSON 库,怎么选择是一个难题. [nativejson-benchmark](https://git ...