您的位置:首页 > 编程语言 > Java开发

Java框架spring 学习笔记(十九):事务管理(注解管理)

2017-11-10 16:36 453 查看
注解管理的方式要比xml配置方式要简单很多

只需在配置文件中添加事务注解

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3        xmlns:tx="http://www.springframework.org/schema/tx"
4        xmlns:aop="http://www.springframework.org/schema/aop"
5        xmlns:context="http://www.springframework.org/schema/context"
6        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7        xsi:schemaLocation="
8             http://www.springframework.org/schema/beans 9             http://www.springframework.org/schema/beans/spring-beans.xsd 10             http://www.springframework.org/schema/context 11             http://www.springframework.org/schema/context/spring-context.xsd 12             http://www.springframework.org/schema/tx 13             http://www.springframework.org/schema/tx/spring-tx.xsd 14             http://www.springframework.org/schema/aop 15             http://www.springframework.org/schema/aop/spring-aop.xsd ">
16
17     <!-- 配置c3p0连接池 -->
18     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
19         <!-- 注入dao对象 -->
20         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
21         <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
22         <property name="user" value="root"></property>
23         <property name="password" value="jqbjqbjqb123"></property>
24     </bean>
25
26     <bean id="orderService" class="cn.service.OrderService">
27         <property name="orderDao" ref="orderDao"></property>
28     </bean>
29     <bean id="orderDao" class="cn.dao.OrderDao">
30         <!-- 注入jdbcTemplate对象-->
31         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
32     </bean>
33
34     <!-- 创建jdbcTemplate对象 -->
35     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
36         <!-- 把dataSource传递到模板对象中-->
37         <property name="dataSource" ref="dataSource"></property>
38     </bean>
39
40     <!-- 第一步:配置事务管理器 -->
41     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
42         <!-- 注入dataSource -->
43         <property name="dataSource" ref="dataSource"></property>
44     </bean>
45
46     <!-- 第二步:开启事务注解 -->
47     <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
48 
49 </beans>


然后在逻辑业务类上加上注解@Transactional 即可

1 package cn.service;
2
3 import cn.dao.OrderDao;
4 import org.springframework.transaction.annotation.Transactional;
5
 6 @Transactional
 7 public class OrderService {
8     private OrderDao orderDao;
9
10     public void setOrderDao(OrderDao orderDao) {
11         this.orderDao = orderDao;
12     }
13
14     //调用dao的方法
15     //业务逻辑层,写转账业务
16     public void accountMoney(){
17         //狗蛋转账给建国,在账面上看就是狗蛋减钱,建国多钱
18         //狗蛋减钱
19         orderDao.lessMoney();
20         int i = 10/0;
21         //建国多钱
22         orderDao.moreMoney();
23     }
24 }


可防止不明错误导致数据产生不一致。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: