spring学习(十一)—事务管理(注解方式实现)
来源:互联网 发布:繁体系统安装简体软件 编辑:程序博客网 时间:2024/05/21 08:47
转载自传智博客学习视频
声明式事务管理配置步骤:
1.在配置文件中配置事务管理器
2.在配置文件中配置事务注解
3.在要使用事务的方法所在类上面添加注解(该类里面的所有方法都会被事务管理)
1.代码结构
2.测试类
package service;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestService { @Test public void testService(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); OrdersService ordersService = (OrdersService) context.getBean("ordersService"); ordersService.accountMoney(); }}
3.服务类
package service;import org.springframework.transaction.annotation.Transactional;import dao.OrderDao;@Transactionalpublic class OrdersService { public OrderDao orderDao; public void setOrderDao(OrderDao orderDao) { this.orderDao = orderDao; } public void accountMoney(){ orderDao.lessMoney(); int i = 10/0; orderDao.moreMoney(); }}
4.数据库操作类
package dao;import org.springframework.jdbc.core.JdbcTemplate;public class OrderDao { public JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void lessMoney() { String sql="update account set salary=salary+? where username=?"; jdbcTemplate.update(sql,-1000,"xiaowang"); } public void moreMoney() { String sql="update account set salary=salary+? where username=?"; jdbcTemplate.update(sql,1000,"xiaoma"); }}
5.配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 注入属性值 --> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///springlearn"></property> <property name="user" value="root"></property> <property name="password" value="root"></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"/> <bean id="ordersService" class="service.OrdersService"> <property name="orderDao" ref="orderDao"></property> </bean> <bean id="orderDao" class="dao.OrderDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!-- 创建jdbc模板对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean></beans>
0 0
- spring学习(十一)—事务管理(注解方式实现)
- Spring声明式事务管理(基于注解方式实现)
- spring 声明式事务管理注解方式实现
- spring学习(十)—事务管理(配置文件方式实现)
- Spring学习8-Spring事务管理(注解式声明事务管理)
- Spring学习8-Spring事务管理(注解式声明事务管理)
- Spring学习8-Spring事务管理(注解式声明事务管理)
- Spring注解方式进行事务管理
- spring的事务管理(配置文件方式和注解方式实现、转账环境搭建)
- spring声明式事务管理(注解实现)
- 15-spring的事务管理(注解实现)
- spring中注解方式实现声明式事务管理
- spring学习笔记(7)——spring事务管理方式
- spring 事务管理实现方式
- Spring事务管理方式实现
- Spring 事务管理(基于xml方式和注解方式)
- spring注解实现事务管理问题
- Hibernate学习4 二级缓存强化和事务管理 注解方式实现
- 欢迎使用CSDN-markdown编辑器
- 一个计算剩余时间的php自定义函数
- 一些关于当前时间的限制
- AngularJS+Jersey下载excel
- react+redux框架配置从无到有直到正常运行全流程(上)
- spring学习(十一)—事务管理(注解方式实现)
- Android的init过程(二):初始化语言(init.rc)解析
- 七种css方式让容器垂直水平居中
- 好的软件架构设计
- QT Demo 之 window(4) ScreenInfo
- 朴素贝叶斯
- python 文件操作
- Android Service最全面的解析
- ELK多种架构及优劣