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. 事务简介: 事务 ...
随机推荐
- SAP 740GUI客户端快捷方式取消密码登陆
客户端电脑:WIN10 专业版64位. 1. 进入注册表编辑器(regedit) .进入如下路径:HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\SAP\SAPLogo ...
- deque时间复杂度和vector,list比较
deque supports const time insert and erase operations at the beginning or the end, insert or erase i ...
- 牛客寒假算法基础集训营4 B applese 走方格
链接:https://ac.nowcoder.com/acm/contest/330/B 构造题,但是有两个特判... 1 2 2 1 然后就水了,血亏 #include<stdio.h&g ...
- zabbix监控java内存的脚本
#!/bin/bash # 截取java的pid号 java_pid=`netstat -lnpt |grep |awk -F '/' '{print $1'}` # 截取$jstat命令的位置 js ...
- codeforces-473D Mahmoud and Ehab and another array construction task (素数筛法+贪心)
题目传送门 题目大意:先提供一个数组,让你造一个数组,这个数组的要求是 1 各元素之间都互质 2 字典序大于等于原数组 3 每一个元素都大于2 思路: 1.两个数互质的意思就是没有公因子.所以每 ...
- http转https的各种应用
http://www.lccee.com/content-57.html https://www.gworg.com/ssl/127.html apach: LoadModule socache_sh ...
- 原生JS实现图片拖拽移动与缩放
看一下最终效果,图片可以拖动,可以缩放 把代码贴出来,可以直接粘贴使用,大致的思想就是鼠标按下的时候获取当时的鼠标位置,要减去left和top值,移动的时候获取位置减去初始的值就得到移动的时候的lef ...
- Yii2 urles
public function rules(){ return [ //必须填写 ['email, username, password,agree,verifyPassword,verifyCode ...
- Checkstyle的配置详解
Checkstyle是一款检查java程序代码样式的工具,可以有效的帮助我们检视代码以便更好的遵循代码编写标准,特别适用于小组开发时彼此间的样式规范和统一.Checkstyle提供了高可配置性,以便适 ...
- 前端技巧-w3c
1.使用全等===比较符 if (zeroAsAString === 0) { // 判断为false }在和null进行比较的时候,允许使用 == 比较符 2.使用 .parseInt() 的时候, ...