一。创建数据库

二。建立OrderDao OrderService 和测试类

package springcount;import org.springframework.transaction.annotation.Transactional;
import c3p0.UserDao;
public class OrderService {private OrderDao orderDao;public OrderDao getOrderDao() {return orderDao;}public void setOrderDao(OrderDao orderDao) {this.orderDao = orderDao;}public void count(){//小明账户减少orderDao.lessmoney();int i=10/0;//小李账户增加orderDao.moremoney();}
}
package springcount;import org.springframework.jdbc.core.JdbcTemplate;public class OrderDao {private JdbcTemplate jdbcTemplate;public JdbcTemplate getJdbcTemplate() {return jdbcTemplate;}public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}public void lessmoney(){String sql="update test3 set sarlary=sarlary-? where username=?";jdbcTemplate.update(sql,"1000","xiaowang");}public void moremoney(){String sql="update test3 set sarlary=sarlary+? where username=?";jdbcTemplate.update(sql,"1000","xiaoli");}
}
package springcount;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestCount {@Testpublic void testCount(){ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext2.xml");OrderService orderService=(OrderService)context.getBean("orderService");orderService.count();}
}

三。写applicationContext2.xml文件(基于xml方式)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-3.1.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"><!-- 配置c3po连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://120.27.104.36:3306/test"></property><property name="user" value="hpn"></property><property name="password" value="hpn2017"></property></bean><!--第一步,配置事务管理器  --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--注入dataSource  --><property name="dataSource" ref="dataSource"></property></bean><!--第二步,配置事务增强  --> <tx:advice id="txadvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="count"/></tx:attributes></tx:advice><!--第三步,配置切面  --> <aop:config><aop:pointcut expression="execution(* springcount.OrderService.*(..))" id="pointcut1"/><aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/></aop:config><bean id="orderDao" class="springcount.OrderDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="orderService" class="springcount.OrderService"><!-- 注入dao对象 --><property name="orderDao" ref="orderDao"></property>       </bean><!-- 创建jdbcTemplate对象 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean>
</beans>

基于注解方式:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-3.1.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"><!-- 配置c3po连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://120.27.104.36:3306/test"></property><property name="user" value="hpn"></property><property name="password" value="hpn2017"></property></bean><!--第一步,配置事务管理器  --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--注入dataSource  --><property name="dataSource" ref="dataSource"></property></bean><tx:annotation-driven transaction-manager="transactionManager"/><!--第二步,配置事务增强  -->
<!--     <tx:advice id="txadvice" transaction-manager="transactionManager"> -->
<!--         <tx:attributes> -->
<!--             <tx:method name="count"/> -->
<!--         </tx:attributes> -->
<!--     </tx:advice> --><!--第三步,配置切面  -->
<!--     <aop:config> -->
<!--         <aop:pointcut expression="execution(* springcount.OrderService.*(..))" id="pointcut1"/> -->
<!--         <aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/> -->
<!--     </aop:config> --><bean id="orderDao" class="springcount.OrderDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="orderService" class="springcount.OrderService"><!-- 注入dao对象 --><property name="orderDao" ref="orderDao"></property>     </bean><!-- 创建jdbcTemplate对象 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean>
</beans>

另外还需要在OrderService类前加注释@

@Transactional
public class OrderService {private OrderDao orderDao;public OrderDao getOrderDao() {return orderDao;}public void setOrderDao(OrderDao orderDao) {this.orderDao = orderDao;}public void count(){//小明账户减少orderDao.lessmoney();int i=10/0;//小李账户增加orderDao.moremoney();}
}

Spring 事务管理(基于xml方式和注解方式)相关推荐

  1. Spring事务管理——使用XML配置声明式事务

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 一.事务介绍 事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和 ...

  2. Spring事务管理之几种方式实现事务

    1.事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...

  3. @Spring事务管理

    Spring虽然提供了灵活方便的事务管理功能,但是这些功能都是基于底层数据库本身的事务处理机制工作的,要深入了解Spring的事务管理和配置,有必要对数据库事务的基础知识进行学习! 数据库事务(简称: ...

  4. Spring事务管理理解

    文章目录 一.事务认识 原子性(Atomicity) 一致性(Consistency) 隔离性(Isolation) 持久性(Durability) 二.事务的传播特性 三.事务的隔离级别 四.spr ...

  5. Spring事务管理的总结

    1.事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...

  6. Spring IOC容器-Bean管理——基于XML方式

    Spring IOC容器-Bean管理--基于XML(续集) 1.IOC 操作 Bean 管理(FactoryBean) ​ 1).Spring 有两种类型 bean,一种普通 bean,另外一种工厂 ...

  7. Spring事务管理详解_基本原理_事务管理方式

    Spring事务管理详解_基本原理_事务管理方式 1. 事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,使用JDBC的事务管理机制,就是利用java.sql.Connection对象 ...

  8. spring框架:简述AOP的使用(xml方式和注解方式)

    本人小白一枚,欢迎大家一起讨论学习,如有错误,还望大家指教. AOP概述 AOP的概念: AOP,全称Apect Oriented Programming,译为面向切面编程,简单的说它可以帮我们把程序 ...

  9. spring事务管理的两种方式

    一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2.事务配置实例 (1).spring+mybatis 事务配置 &l ...

  10. Spring事务管理(注解式声明事务管理)备忘

    2019独角兽企业重金招聘Python工程师标准>>> 步骤一.在spring配置文件中引入<tx:>命名空间 <beans  xmlns="http:/ ...

最新文章

  1. hbase shell
  2. java object转list_这份Java开发规范,让你100%受益!
  3. 【BZOJ】2982 combination
  4. mysql57服务无法启动_将mysqld.service服务加入到systemctl
  5. 兼容性测试需要注意的测试点
  6. python tkinter获取屏幕大小_用 Python 制作关不掉的端午安康弹窗
  7. 2018 KDD CUP支付宝安全团队Deep X斩获两项大奖
  8. java安装后在哪里打开_java安装后怎么打开教程
  9. 88个塑胶模具设计中常用知识点!
  10. OPENCV Linux 显示中文 arm64
  11. SAN存储交换机配置
  12. 商业银行数字化转型的难点与路径
  13. mfs java_mfs权威指南
  14. 关于短信群发的简单实现
  15. 最强蜗牛服务器维护祷告bug,最强蜗牛 这个游戏的玩家太难带了,竟然天天希望服务器有事...
  16. Web服务器之Tomcat大全
  17. Design Compiler - DC综合的流程(三)
  18. vulnhub Loly: 1
  19. 【算法千题案例】每日一练LeetCode打卡——108.独特的电子邮件地址
  20. miniui的学习笔记(二)

热门文章

  1. CGI的介绍及简单应用
  2. Sketchup创建光滑曲面
  3. Axure RP 8--成绩管理页面原型图
  4. 动作捕捉软件系统有那么重要吗?
  5. kmeans设置中心_kmeans算法
  6. 航信税控系统 - 安装分析(一)
  7. 数据库事务隔离级别与锁
  8. 单核工作法图解:事多到事少,拖延变高效
  9. js实现删除页面元素
  10. 【Dll调试】DLL调试方法