一.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-事务管理的更多相关文章

  1. Spring笔记:事务管理

    Spring笔记:事务管理 事务管理 Spring事务管理是通过SpringAOP去实现的.默认情况下Spring在执行方法抛出异常后,引发事务回顾,当然你可以用拦截器或者配置去改变它们. 这部门内容 ...

  2. spring 声明式事务管理

    简单理解事务: 比如你去ATM机取5000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉5000元钱:然后ATM出5000元钱.这两个步骤必须是要么都执行要么都不执行.如果银行卡扣除了5000块但 ...

  3. Spring声明式事务管理基于@Transactional注解

    概述:我们已知道Spring声明式事务管理有两种常用的方式,一种是基于tx/aop命名空间的xml配置文件,另一种则是基于@Transactional 注解.         第一种方式我已在上文为大 ...

  4. Spring声明式事务管理基于tx/aop命名空间

    目的:通过Spring AOP 实现Spring声明式事务管理; Spring支持编程式事务管理和声明式事务管理两种方式. 而声明式事务管理也有两种常用的方式,一种是基于tx/aop命名空间的xml配 ...

  5. Spring对Hibernate事务管理

    谈Spring事务管理之前我们想一下在我们不用Spring的时候,在Hibernate中我们是怎么进行数据操作的.在Hibernate中 我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据 ...

  6. spring 编程式事务管理和声明式事务管理

    编程式事务管理 Spring 的编程式事务管理概述 在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择.用过 Hibernate 的人都知道,我们需要在代码中显式调用be ...

  7. Spring学习8-Spring事务管理

      http://blog.sina.com.cn/s/blog_7ffb8dd501014e0f.html   Spring学习8-Spring事务管理(注解式声明事务管理) 标签: spring注 ...

  8. Spring中的事务管理

    事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当作一个单独的工作单元.这些动作要么全部完成,要么全部不起作用 事务的四个关键属性( ...

  9. Spring对Hibernate事务管理【转】

    在谈Spring事务管理之前我们想一下在我们不用Spring的时候,在Hibernate中我们是怎么进行数据操作的.在Hibernate中我们每次进行一个操作的的时候我们都是要先开启事务,然后进行数据 ...

  10. Spring中的事务管理详解

    在这里主要介绍Spring对事务管理的一些理论知识,实战方面参考上一篇博文: http://www.cnblogs.com/longshiyVip/p/5061547.html 1. 事务简介: 事务 ...

随机推荐

  1. Python中的Numpy包

    通过本次学习你可以掌握Numpy Numpy介绍(获取地址)更多Numpy函数 numpy的主要对象是同质多维数组.也就是在一个元素(通常是数字)表中,元素的类型都是相同的. numpy的数组类被成为 ...

  2. linux中断和异常睡眠问题

    中断和异常: 中断只代表异步中断,异常代表同步中断,这样系统调用是异常处理,不是中断处理. 这里异常处理是可以休眠block的,因为异常处理所需的数据是存储在异常栈中,而每个进程都有一个异常栈,所以异 ...

  3. 【学习笔记】Python 3.6模拟输入并爬取百度前10页密切相关链接

    [学习笔记]Python 3.6模拟输入并爬取百度前10页密切相关链接 问题描述 通过模拟网页,实现百度搜索关键词,然后获得网页中链接的文本,与准备的文本进行比较,如果有相似之处则代表相关链接. me ...

  4. http协议&接口规范&接口测试入门

    http协议 请求: 请求行:请求方法.url(协议名://ip;端口/工程名/资源路径).协议版本 请求头 :键值对 请求正文 响应: 响应行:协议版本.响应状态码.响应状态码描述 响应头 :键值对 ...

  5. NorFlash基础

    1. Nor Flash 简介 Nor Flash 闪速存储器具有可靠性高.随机读取速度快的优势,在擦出和编程操作较少而直接执行代码的场合,尤其是纯代码存储的应用中广泛使用. 2. Nor Flash ...

  6. 服务器报nginx: [warn] conflicting server name "blog.xueyi.com" on 0.0.0.0:80, ignored

    nginx: [warn] conflicting server name "blog.xueyi.com" on 0.0.0.0:80, ignored 修改nginx配置参数后 ...

  7. 创建第一个vue工程

    vue创建项目(npm安装→初始化项目) 第一步npm安装 首先:先从nodejs.org中下载nodejs   图1 双击安装,在安装界面一直Next   图2   图3   图4 直到Finish ...

  8. SpringMVC中RequetContextListener

    来自:https://www.cnblogs.com/softidea/p/7068196.html 零.引言 RequetContextListener从名字结尾Listener来看就知道属于监听器 ...

  9. Eclipse代码规范工具-Checkstyle安装和使用

    您首先可以参考这里:http://www.ibm.com/developerworks/cn/java/j-ap01117/index.html 那么首先您应该下载CheStyle: http://s ...

  10. spark项目打jar包,不包含依赖包问题的解决方案

    mvn clean package打包maven-archetype-webapp项目时,打包后的jar包含项目中引用的jar包(解压后,在WEB-INF有一个lib目录,该目录下有所有依赖包). m ...