Spring之事务操作(注解)
事务操作步骤:
<!-- 第一步、配置事务管理器 -->
<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之事务操作(注解)的更多相关文章
- spring的事务操作(重点)
这篇文章一起来回顾复习下spring的事务操作.事务是spring的重点, 也是面试的必问知识点之一. 说来这次面试期间,也问到了我,由于平时用到的比较少,也没有关注过这一块的东西,所以回答的不是特别 ...
- spring的事务操作
我们项目一期已经差不多结束了,所以一些细节也被拿了出来,出现最多的就是事务的操作了.因为自己负责的是一个模块(因为是另外一个项目的负责人),所以组员经常会遇到事务的问题,会出现很多奇葩的用法,各种乱用 ...
- 对于spring中事务@Transactional注解的理解
现在spring的配置都喜欢用注解,这边就说下@Transactional 一.如何开启@Transactional支持 要使用@Transactional,spring的配置文件applicatio ...
- Spring3:spring的事务操作
三.事务操作 1.导包 2. jdbc模板与开源连接池(DBCP与C3P0) 2.1DBCP 2.2C3P0 :: 2.3.抽取配置到属性文件 定义一个属性文件 在Spring的配置文件中引入属 ...
- Spring初学之spring的事务管理注解
spring的事务管理,本文的例子是:比如你需要网购一本书,卖书的那一方有库存量以及书的价格,你有账户余额.回想我们在编程中要实现买书这样的功能,由于你的账户表和书的库存量表肯定不是同一张数据库表,所 ...
- Spring之事务操作(配置文件)
UserDao.java package helloworld.tx; import org.springframework.jdbc.core.JdbcTemplate; public class ...
- Spring中的事务操作
事务的特性 原子性:强调事务的不可分割. 一致性:事务的执行的前后数据的完整性保持一致. 隔离性:一个事务执行的过程中,不应该受到其他事务的干扰. 持久性:事务一旦结束,数据就持久化到数据库. 如果不 ...
- (转)Spring中的事务操作
http://blog.csdn.net/yerenyuan_pku/article/details/70024364 事务的回顾 什么是事务 事务是逻辑上的一组操作,组成这组操作的各个逻辑单元,要么 ...
- Spring 中的事务操作、注解、以及 XML 配置
事务 事务全称叫数据库事务,是数据库并发控制时的基本单位,它是一个操作集合,这些操作要么不执行,要么都执行,不可分割.例如我们的转账这个业务,就需要进行数据库事务的处理. 转账中至少会涉及到两条 SQ ...
随机推荐
- IN2REG group 的时序分析
针对 IN2REG 的 timing group,其 timing 模型是假设 input pin 外面有一个虚拟的reg(如图中的 reg1),这个虚拟reg的 clock 是 virtual cl ...
- Node基础知识点--学习笔记(一)
一:建立http服务器: 在D盘建立一个文件夹node,放入app.js,代码如下: var http = require('http'); http.createServer(function(re ...
- Ubuntu双系统无法挂载Windows10 硬盘的解决方法
我的电脑是在Windows 10下安装的Ubuntu 14.04双系统,今天进入Ubuntu系统访问Windows 10 磁盘,出现如下错误: Error mounting /dev/sda1 at ...
- kubernetes 禁用虚拟内存 swapoff -a ----- 顺便复习sed 命令
1.如果不关闭swap,就会在kubeadm初始化Kubernetes的时候报错,如下图: [ERROR Swap]: running with swap on is not supported. P ...
- day41
今日内容: 1.完整查询语句 2.多表查询 3.子查询 1.完整查询语句: 首先对于昨天的学习补充一个复制表 示例:首先我在一个库中创建了一个t1表(id 为int类型 设置为主键 并且设置了自增描述 ...
- 20155302《网络对抗》Exp4 恶意代码分析
20155302<网络对抗>Exp4 恶意代码分析 实验要求 •是监控你自己系统的运行状态,看有没有可疑的程序在运行. •是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工 ...
- WPF应用
代码 private void button1_Click(object sender, RoutedEventArgs e) { calculate sa = new calculate(int.P ...
- Jmeter 安装后无法启动问题
问题:按照教程java环境安装完成,也下载了 Jmeter 安装包,但是在启动的时候 dos窗口就一直提示下面的错误信息 ‘findstr' 不是内部或外部命令,也不是可运行的程序或批处理文件. ...
- Luogu P1120 小木棍 [数据加强版]
看了题目心中只有一个字——搜索!!! 但是很显然,朴素的搜索(回溯)绝壁超时. 剪枝&优化(要搞很多,要不然过不了) 1:从小到大搜索它们的因数,这样找到就exit. 2:将数据从大到小排序, ...
- LoRa---射频信号接收框架简图介绍
LoRa整体框架图如下: 内容下次再填! 内容补上: 射频信号的接收流程(小博并非专业,错了emmmmm轻喷):射频--->中频--->基带,下面按照图中标的序号开始介绍: 1.天线接受射 ...