spring笔记4-事务管理
一.xml配置文件形式
通过转账案例,学习事务管理
1.建立数据库
2.编写entity
1 package huguangqin.com.cnblogs.entity;
2
3 public class User {
4 private Integer id;
5 private String name;
6 private Double money;
7
8 public User() {
9 super();
10 }
11
12 public User(Integer id, String name, Double money) {
13 super();
14 this.id = id;
15 this.name = name;
16 this.money = money;
17 }
18
19 public Integer getId() {
20 return id;
21 }
22
23 public void setId(Integer id) {
24 this.id = id;
25 }
26
27 public String getName() {
28 return name;
29 }
30
31 public void setName(String name) {
32 this.name = name;
33 }
34
35 public Double getMoney() {
36 return money;
37 }
38
39 public void setMoney(Double money) {
40 this.money = money;
41 }
42
43 }
44
3.编写dao
1 package huguangqin.com.cnblogs.dao;
2
3 public interface UserDao {
4 void increasement(int id, double money);
5
6 void decreasement(int id, double money);
7 }
8
4.编写daoImpl
1 package huguangqin.com.cnblogs.daoImpl;
2
3 import org.springframework.jdbc.core.support.JdbcDaoSupport;
4
5 import huguangqin.com.cnblogs.dao.UserDao;
6
7 public class UserDaoImpl extends JdbcDaoSupport implements UserDao {
8
9 @Override
10 public void increasement(int id, double money) {
11 String sql = "update t_bank set money=money+? where id = ?";
12 getJdbcTemplate().update(sql, money, id);
13 }
14
15 @Override
16 public void decreasement(int id, double money) {
17 String sql = "update t_bank set money=money-? where id = ?";
18 getJdbcTemplate().update(sql, money, id);
19 }
20
21 }
22
5.编写service
1 package huguangqin.com.cnblogs.service;
2
3 public interface IUserService {
4 void tranfer(int where, int to, double money);
5 }
6
6.编写serviceImpl
1 package huguangqin.com.cnblogs.serviceImpl;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4
5 import huguangqin.com.cnblogs.dao.UserDao;
6 import huguangqin.com.cnblogs.service.IUserService;
7
8 public class UserServiceImpl implements IUserService {
9 //调用UserDao操作数据库,spring下调用接口,并注入实例对象
10 @Autowired
11 private UserDao ud;
12
13 public void setUd(UserDao ud) {
14 this.ud = ud;
15 }
16
17 @Override
18 public void tranfer(int where, int to, double money) {
19 ud.decreasement(where, money);
20 ud.increasement(to, money);
21 }
22 }
23
7.编写db.properties
1 jdbc.driverClass=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql:///spring_day02
3 jdbc.user=root
4 jdbc.password=root
8.编写applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <beans
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns="http://www.springframework.org/schema/beans"
6 xmlns:context="http://www.springframework.org/schema/context"
7 xmlns:aop="http://www.springframework.org/schema/aop"
8 xmlns:tx="http://www.springframework.org/schema/tx"
9 xsi:schemaLocation="http://www.springframework.org/schema/beans
10 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
11 http://www.springframework.org/schema/context
12 http://www.springframework.org/schema/context/spring-context-4.2.xsd
13 http://www.springframework.org/schema/aop
14 http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
15 http://www.springframework.org/schema/tx
16 http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
17
18 <!--读取db.propertiey -->
19 <context:property-placeholder location="classpath:db.properties"/>
20
21 <!-- 配置连接池到spring容器 -->
22 <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
23 <property name="driverClass" value="${jdbc.driverClass}"></property>
24 <property name="jdbcUrl" value="${jdbc.url}"></property>
25 <property name="user" value="${jdbc.user}"></property>
26 <property name="password" value="${jdbc.password}"></property>
27 </bean>
28
29 <!-- 配置核心事务管理器 -->
30 <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
31 <property name="dataSource" ref="dataSource"></property>
32 </bean>
33
34 <!-- 配置事务管理通知 -->
35 <tx:advice id="txAdvice" transaction-manager="transactionManager">
36 <tx:attributes>
37 <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
38 <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
39 <tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
40 </tx:attributes>
41 </tx:advice>
42
43 <!-- 配置织入 -->
44 <aop:config>
45 <aop:pointcut expression="execution(* huguangqin.com.cnblogs.serviceImpl.*ServiceImpl.*(..))" id="txPc"/>
46 </aop:config>
47
48 <!-- 配置dao -->
49 <bean name="userDao" class="huguangqin.com.cnblogs.daoImpl.UserDaoImpl">
50 <property name="dataSource" ref="dataSource"></property>
51 </bean>
52
53 <!-- 配置service -->
54 <bean name="userService" class="huguangqin.com.cnblogs.serviceImpl.UserServiceImpl">
55 <property name="ud" ref="userDao"></property>
56 </bean>
57 </beans>
58
二.Annotation形式
与xml相比需修改以下文件
1.applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <beans
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns="http://www.springframework.org/schema/beans"
6 xmlns:context="http://www.springframework.org/schema/context"
7 xmlns:aop="http://www.springframework.org/schema/aop"
8 xmlns:tx="http://www.springframework.org/schema/tx"
9 xsi:schemaLocation="http://www.springframework.org/schema/beans
10 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
11 http://www.springframework.org/schema/context
12 http://www.springframework.org/schema/context/spring-context-4.2.xsd
13 http://www.springframework.org/schema/aop
14 http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
15 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
16
17 <!--读取db.propertiey -->
18 <context:property-placeholder location="classpath:db.properties"/>
19
20 <!-- 配置连接池到spring容器 -->
21 <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
22 <property name="driverClass" value="${jdbc.driverClass}"></property>
23 <property name="jdbcUrl" value="${jdbc.url}"></property>
24 <property name="user" value="${jdbc.user}"></property>
25 <property name="password" value="${jdbc.password}"></property>
26 </bean>
27
28 <!-- 配置核心事务管理器 -->
29 <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
30 <property name="dataSource" ref="dataSource"></property>
31 </bean>
32
33 <!--开启注解管理事务开关 -->
34 <tx:annotation-driven transaction-manager="transactionManager"/>
35
36 <!-- 配置dao -->
37 <bean name="userDao" class="huguangqin.com.cnblogs.daoImpl.UserDaoImpl">
38 <property name="dataSource" ref="dataSource"></property>
39 </bean>
40
41 <!-- 配置service -->
42 <bean name="userService" class="huguangqin.com.cnblogs.serviceImpl.UserServiceImpl">
43 <property name="ud" ref="userDao"></property>
44 </bean>
45 </beans>
46
2.serviceImpl类
1 package huguangqin.com.cnblogs.serviceImpl;
2
3 import org.springframework.beans.factory.annotation.Autowired;
4 import org.springframework.transaction.annotation.Transactional;
5
6 import huguangqin.com.cnblogs.dao.UserDao;
7 import huguangqin.com.cnblogs.service.IUserService;
8
9 @Transactional
10 public class UserServiceImpl implements IUserService {
11 @Autowired
12 private UserDao ud;
13
14 public UserDao getUd() {
15 return ud;
16 }
17
18 public void setUd(UserDao ud) {
19 this.ud = ud;
20 }
21
22 @Override
23 // @Transactional(isolation = Isolation.DEFAULT, propagation =Propagation.REQUIRED, readOnly = false)
24 public void tranfer(int where, int to, double money) {
25 ud.decreasement(where, money);
26 ud.increasement(to, money);
27 }
28
29 }
30
spring笔记4-事务管理的更多相关文章
- Spring笔记:事务管理
Spring笔记:事务管理 事务管理 Spring事务管理是通过SpringAOP去实现的.默认情况下Spring在执行方法抛出异常后,引发事务回顾,当然你可以用拦截器或者配置去改变它们. 这部门内容 ...
- spring 声明式事务管理
简单理解事务: 比如你去ATM机取5000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉5000元钱:然后ATM出5000元钱.这两个步骤必须是要么都执行要么都不执行.如果银行卡扣除了5000块但 ...
- Spring声明式事务管理基于@Transactional注解
概述:我们已知道Spring声明式事务管理有两种常用的方式,一种是基于tx/aop命名空间的xml配置文件,另一种则是基于@Transactional 注解. 第一种方式我已在上文为大 ...
- Spring声明式事务管理基于tx/aop命名空间
目的:通过Spring AOP 实现Spring声明式事务管理; Spring支持编程式事务管理和声明式事务管理两种方式. 而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配 ...
- Spring对Hibernate事务管理
谈Spring事务管理之前我们想一下在我们不用Spring的时候,在Hibernate中我们是怎么进行数据操作的.在Hibernate中 我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据 ...
- spring 编程式事务管理和声明式事务管理
编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...
- Spring学习8-Spring事务管理
http://blog.sina.com.cn/s/blog_7ffb8dd501014e0f.html Spring学习8-Spring事务管理(注解式声明事务管理) 标签: spring注 ...
- Spring中的事务管理
事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当作一个单独的工作单元.这些动作要么全部完成,要么全部不起作用 事务的四个关键属性( ...
- Spring对Hibernate事务管理【转】
在谈Spring事务管理之前我们想一下在我们不用Spring的时候,在Hibernate中我们是怎么进行数据操作的.在Hibernate中我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据 ...
- Spring中的事务管理详解
在这里主要介绍Spring对事务管理的一些理论知识,实战方面参考上一篇博文: http://www.cnblogs.com/longshiyVip/p/5061547.html 1. 事务简介: 事务 ...
随机推荐
- P4088 [USACO18FEB]Slingshot 线段树+扫描线
\(\color{#0066ff}{ 题目描述 }\) Farmer John最讨厌的农活是运输牛粪.为了精简这个过程,他产生了一个新奇的想法:与其使用拖拉机拖着装满牛粪的大车从一个地点到另一个地点, ...
- Laplace(拉普拉斯)算子
[摘要] Laplace算子作为边缘检测之一,和Sobel算子一样也是工程数学中常用的一种积分变换,属于空间锐化滤波操作.拉普拉斯算子(Laplace Operator)是n维欧几里德空间中的一个二阶 ...
- Qt 学习之路 2(26):反走样
Qt 学习之路 2(26):反走样 豆子 2012年11月12日 Qt 学习之路 2 9条评论 我们在光栅图形显示器上绘制非水平.非垂直的直线或多边形边界时,或多或少会呈现锯齿状外观.这是因为直线和多 ...
- Python: 安装 sklearn 包出现错误的解决方法
今天在安装 Python 的 sklearn 包时出现了 Cannot uninstall 'numpy' 和 Cannot uninstall 'scipy' 错误,下面记录了我尝试了很多网上的方法 ...
- CSS趣味
谈一下小技巧: 1.先看一下问题,实现下图,只用于一个html元素有多少种实现方式? 假设我们的单标签是一个 div: <div></div> 定义如下通用CSS: div{ ...
- 1005 继续(3n+1)猜想(25 分)
卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对 n=3 进行验证的时 ...
- yii2 basic VER
assets/ contains assets definition 包含资源定义 commands/ contains console commands (controllers) 包含命令行命令, ...
- AD域账号验证
public partial class _Default : Page { [DllImport("advapi32.dll")] private static extern b ...
- java web关于文件上传下载的总结
文件上传使用<form method="POST" enctype="multipart/form-data"> , 而不是默认的applica ...
- docker基本命令日志
docker run - Run a command in a new container 启动一个新的容器,一般在docker pull之后首次运行此image -i 保持stdout打开 -t 打 ...