Spring之事务操作(配置文件)
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之事务操作(配置文件)的更多相关文章
- spring的事务操作(重点)
		这篇文章一起来回顾复习下spring的事务操作.事务是spring的重点, 也是面试的必问知识点之一. 说来这次面试期间,也问到了我,由于平时用到的比较少,也没有关注过这一块的东西,所以回答的不是特别 ... 
- Spring3:spring的事务操作
		三.事务操作 1.导包 2. jdbc模板与开源连接池(DBCP与C3P0) 2.1DBCP 2.2C3P0 :: 2.3.抽取配置到属性文件 定义一个属性文件 在Spring的配置文件中引入属 ... 
- spring的事务操作
		我们项目一期已经差不多结束了,所以一些细节也被拿了出来,出现最多的就是事务的操作了.因为自己负责的是一个模块(因为是另外一个项目的负责人),所以组员经常会遇到事务的问题,会出现很多奇葩的用法,各种乱用 ... 
- Spring之事务操作(注解)
		事务操作步骤: <!-- 第一步.配置事务管理器 --> <bean id="transactionManager" class="org.spring ... 
- Spring中的事务操作
		事务的特性 原子性:强调事务的不可分割. 一致性:事务的执行的前后数据的完整性保持一致. 隔离性:一个事务执行的过程中,不应该受到其他事务的干扰. 持久性:事务一旦结束,数据就持久化到数据库. 如果不 ... 
- (转)Spring中的事务操作
		http://blog.csdn.net/yerenyuan_pku/article/details/70024364 事务的回顾 什么是事务 事务是逻辑上的一组操作,组成这组操作的各个逻辑单元,要么 ... 
- Spring Framework 中启动 Redis 事务操作
		背景: 项目中遇到有一系列对Redis的操作,并需要保持事务处理. 环境: Spring version 4.1.8.RELEASE Redis Server 2.6.12 (64位) spring- ... 
- Spring事务操作介绍
		Spring的特色之一,简单而强大的事务管理功能,包括编程式事务和声明式事务. 1. Spring中涉及到事务管理的API有100多个,核心的只有三个: TransactionDefinition.P ... 
- spring学习(三) ———— spring事务操作
		前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ... 
随机推荐
- JS日历控件优化(增加时分秒)
			JS日历控件优化 在今年7月份时候 写了一篇关于 "JS日历控件" 的文章 , 当时只支持 年月日 的日历控件,现在优化如下: 1. 在原基础上 支持 yyyy ... 
- Jquery回到顶部功能
			问题描述: 在网页中,我们经常会由于网页内容过长,而需要在浏览网页时有一个快速回到网页顶部的功能,在浏览网页内容离顶部有一段距离时,出现快速回到网页顶部的工具,从而能使我们的网页更人性化. 问题的产生 ... 
- 【转】SQL 常用关键字释义和用法
			转自: http://blog.csdn.net/iamwangch/article/details/8093933 下面 是 从网络上整理 出来的 SQL 关键字和 常用函数的 释义和简单用 ... 
- CentOS配置Hive
			hive搭建共分为三种模式:1.embedded,2.local,3.remote server 在这里,主要是配置第3种模式:remote server模式,如下图所示: 我的环境共三台虚拟机:Ho ... 
- C++之构造函数拷贝
			拷贝构造函数,顾名思义,就是通过拷贝对象的方式创建一个新对象.拷贝构造函数有两种原型(我们继续以book类来说明拷贝构造函数原型): book(book &b); book(const boo ... 
- HDU 4857 逃生(反向建边的拓扑排序+贪心思想)
			逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ... 
- 20155310 Exp6 信息收集与漏洞扫描
			20155310 Exp6 信息收集与漏洞扫描 基础问题回答 1.哪些组织负责DNS,IP的管理. 顶级的管理者是Internet Corporation for Assigned Names and ... 
- Hibernate一对多关联关系保存时的探究
			在以前使用hibernate时,经常对保存存在关联关系的对象时,不确定是否能保存成功. 因此,特意对一对多关系的2个对象进行实践. 一.pojo类和配置文件的准备 这里有一点提前 ... 
- 【WPF】WPF截屏
			原文:[WPF]WPF截屏 引言 .NET的截图控件在网上流传得不多啊,难得发现一个精品截图控件( 传送门),但是无奈是winform的.后来又找到一个周银辉做的WPF截图(继续传送门),发现截屏是实 ... 
- Redis学习之路(四)之Redis集群
			[toc] #Redis集群 1.Redis Cluster简介 Redis Cluster为Redis官方提供的一种分布式集群解决方案.它支持在线节点增加和减少. 集群中的节点角色可能是主,也可能是 ... 
