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. 事务简介: 事务 ...
随机推荐
- 10.19 qbxt国庆day3
最近的题都莫名简单 经常AK 炼金术 [问题描述] 即使是最伟大的ACM选手也是需要足够的金钱来把妹的的.于是ZYB发明了一台炼金机器. 这台机器一共有三个功能: 1.能把a位沙子变成b位石油. 2. ...
- C++基础学习8:类的定义(class)
先来说说C和C++中结构体的不同 a) C语言中的结构体不能为空,否则会报错(??) b) C语言中内存为空结构体分配大小为0,C++中为结构体和类分配大小为1byte c) C语言中的结构体只涉及到 ...
- C++学习笔记-关键词
1.friend友元 采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一般定义为公有的,依此提供类与外界间的通信接口.但是,有时需要定义一些函数,这些函数不是类的一部分( ...
- CF581D Three Logos 暴力
Three companies decided to order a billboard with pictures of their logos. A billboard is a big squa ...
- angularJs增加行的操作
<!-- 编辑窗口 --> <div class="modal fade" id="editModal" tabindex="-1& ...
- 3D电影转2D普通电影,电脑上看
下了一些电影,打开发现是左右两个一样的画面,什么情况?原来这就是传说中的3D,怎么像正常电影一样的看呢?第一反应去搜3D眼镜,价钱倒是不贵,但是不应急呀,肿么办?以下是观看方法: 一.看电脑上有QQ影 ...
- Apache 403 错误。。
两个方面.. 一: httpd.conf 是否有 <directory '/www'></directory> 是否有 Deny from all 或者 Require l ...
- Linux中***配置
Ubuntu系统下: 1.执行如下命令 sudo apt install shadowsocks polipo 2.创建 shadowsocks.json 配置文件,放在你想放的位置 { " ...
- source vs export AND ctrl d vs ctrl z
在脚本中export,只在当前shell脚本进程和子进程中有效 source的作用中是将export的变量在当前脚本环境生效, 如果是在父脚本中执行source,在子脚本中执行export, 父脚本退 ...
- oracle数据库的备份与还原
转自:https://www.cnblogs.com/ylldbk/p/5613365.html 数据导出: 1 将数据库TEST完全导出,用户名system 密码manager 导出到D:\daoc ...