Spring 事务管理(基于xml方式和注解方式)
来源:互联网 发布:免费录音机软件下载 编辑:程序博客网 时间:2024/05/29 17:57
一。创建数据库
二。建立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类前加注释@
@Transactionalpublic 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();}}
阅读全文
0 0
- Spring 事务管理(基于xml方式和注解方式)
- Spring事务管理(声明式的XML方式和注解方式)
- Spring基于注解方式的事务管理
- Spring 声明式事务管理----基于注解方式
- Spring声明式事务管理(基于注解方式实现)
- spring声明式事务管理(基于AspectJ的xml方式)
- Spring声明式事务管理(基于XML方式实现)
- Spring注解方式进行事务管理
- Spring声明式事务管理(基于注解的方式)
- 基于@Transactional注解方式的spring声明式事务管理
- spring声明式事务管理:基于注解的方式
- Spring声明式事务管理(XML与注解方式)
- spring的事务管理(配置文件方式和注解方式实现、转账环境搭建)
- Spring声明式事务管理(基于xml的方式)
- Spring 声明式事务管理----基于AspectJ的XML方式
- 【spring】AOP xml和注解实现方式
- Spring事务管理(基于注解)
- spring学习(十一)—事务管理(注解方式实现)
- TP5中Db类与Model类关系
- Android 高级控件
- Bzoj3484 [Baltic2012]brackets
- 微信小程序例子——获取手机屏幕亮度
- PHP实现Collection数据集类及其原理
- Spring 事务管理(基于xml方式和注解方式)
- Java类的属性与方法要点
- qsort()排序
- caffe学习笔记2:数据层及其参数
- c语言控制台推箱子小游戏
- spring bean加载过程
- JAX-RS 2.0如何验证查询参数
- Oracle 更新Opatch、打补丁
- 堆和优先级队列