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 ...
随机推荐
- 如何快速找到某个研究领域的所有SCI期刊
https://www.toutiao.com/a6624332265285485060/?tt_from=dingtalk&utm_campaign=client_share&tim ...
- 【php增删改查实例】第六节 - 部门管理模块(开始)
sql建表语句详见:https://www.jianshu.com/p/c88077ed9073 1.新建html模板 新建一个空白的txt文档,然后把后缀名改为.html 用任意一个编辑器打开,比如 ...
- L017-linux系统定时任务crond入门小节
L017-linux系统定时任务crond入门小节 oh my god!how old are you? 怎么老是你?没错,我又来了,哈哈哈,今天是我的生日呢,在这么重要的日子里,必须要更一篇学习小节 ...
- Asp.Net_Form验证跟授权
配置文件的<system.web></system.web>结点下添加如下代码: <!--身份验证方式--> <authentication mode=&qu ...
- JQ_One()函数特效
先看一个例子,当点击 p 元素时,增加该元素的文本大小,代码如下:<script type="text/javascript" src="http://keleyi ...
- 软件测试_APP测试_兼容性测试
APP的兼容测试主要就是测试APP的安装.启动.运行.卸载测试,以及安装时间 .启动时间.CPU占用.内存占用.流量耗用.电量耗用等性能上的测试. 兼容性测试点: 一.内部兼容性: 1.与本地和其他主 ...
- Unity关于方法事件生命周期官方文档
http://docs.unity3d.com/Manual/ExecutionOrder.html 一.组件运行的基本顺序 下图中创建类的顺序为A,B,C,A1,二运行的结果为A1,B,C,A. 可 ...
- 基于Shader实现的UGUI描边解决方案
基于Shader实现的UGUI描边解决方案 前言 大扎好,我系狗猥.当大家都以为我鸽了的时候,我又出现了,这也是一种鸽.创业两年失败后归来,今天想给大家分享一个我最近研究出来的好康的,比游戏还刺激,还 ...
- Jenkins报表 代码 指标分析
Jenkins报表 这表现在前面的章节中,也有可用最简单的一种是适用于 JUnit 测试报告的许多报表插件. 在生成后动作进行任何工作,你可以定义要创建的报告. 该构建已经完成,测试结果选项将可进一步 ...
- win10系统安装web3js的正确方法
在安装web3的时候 用npm install web3 –save-dev 在win10系统下会一直安装不成功.后来换用了 cnpm install web3 –save-dev 安装时候报出:C ...