Spring之事务操作(注解)

事务操作步骤:

    <!-- 第一步、配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 第二步、开启事务的注解 -->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    <!-- 第三步、在事务类上添加注解
    @Transactional
    public class UserSerivce {  -->

 

举例如下:

UserDao.java

 1 package helloworld.txZhuJie;
 2 
 3 import org.springframework.jdbc.core.JdbcTemplate;
 4 
 5 public class UserDao {
 6 
 7     private JdbcTemplate jdbcTemplate;
 8 
 9     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
10         this.jdbcTemplate = jdbcTemplate;
11     }
12 
13     //    减少
14     public void reduce(String name,int num) {
15         // 调用jdbcTemplate对象中的方法实现操作
16         String sql = "update salary set salary = (salary - ?) where name= ?";
17         // 表结构:name(varchar 20),salary(int 20)
18         int rows = jdbcTemplate.update(sql, num, name);
19         System.out.println("修改行数:" + rows);
20     }
21 
22     //    增加
23     public void increase(String name,int num) {
24         // 调用jdbcTemplate对象中的方法实现操作
25         String sql = "update salary set salary = (salary + ?) where name= ?";
26         // 表结构:name(varchar 20),salary(int 20)
27         int rows = jdbcTemplate.update(sql, num, name);
28         System.out.println("修改行数:" + rows);
29     }
30 
31     //    实现添加操作
32     public void add() {
33         // 调用jdbcTemplate对象中的方法实现操作
34         String sql = "insert into salary value(?,?)";
35         // 表结构:name(varchar 20),salary(int 20)
36         int rows = jdbcTemplate.update(sql, "Tom", 10000);
37         System.out.println("插入行数:" + rows);
38         rows = jdbcTemplate.update(sql, "Jerry", 10000);
39         System.out.println("插入行数:" + rows);
40     }
41 
42 }

 

UserSerivce.java

 1 package helloworld.txZhuJie;
 2 
 3 import org.springframework.transaction.annotation.Transactional;
 4 
 5 //事务操作注解
 6 @Transactional
 7 public class UserSerivce {
 8     private UserDao userDao;
 9 
10     public void setUserDao(UserDao userDao) {
11         this.userDao = userDao;
12     }
13 
14     public void add(){
15         userDao.add();
16     }
17 
18 //    工资调整
19     public void updateAccount(){
20 
21         userDao.reduce("Tom",1000);
22 
23 //        制造异常
24 //        int n = 100/0;
25 
26         userDao.increase("Jerry",1000);
27 
28     }
29 
30 }

 

TestTXZhuJie.java

 1 package helloworld.txZhuJie;
 2 
 3 import org.junit.Before;
 4 import org.junit.Ignore;
 5 import org.junit.Test;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 
 9 /*
10 * 事务操作(注解)举例
11 * 1、配置事务管理器
12 * 2、配置事务注解
13 * 3、在使用事务的方法所在类上添加注解
14 * */
15 public class TestTXZhuJie {
16     @Before
17     public void beforeRun() {
18         System.out.println("beforeRun");
19     }
20 
21 //    插入数据
22     @Ignore
23     @Test
24     public void add() {
25         ApplicationContext context =
26                 new ClassPathXmlApplicationContext("beans_tx_zhujie.xml");
27         UserSerivce userSerivce = (UserSerivce) context.getBean("userSerivce");
28         userSerivce.add();
29     }
30 
31     @Test
32     public void update() {
33         ApplicationContext context =
34                 new ClassPathXmlApplicationContext("beans_tx_zhujie.xml");
35         UserSerivce userSerivce = (UserSerivce) context.getBean("userSerivce");
36         userSerivce.updateAccount();
37     }
38 
39 }

 

beans_tx_zhujie.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:contexnt="http://www.springframework.org/schema/context"
 4        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         http://www.springframework.org/schema/context/spring-context-2.5.xsd
 9         http://www.springframework.org/schema/tx
10         http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
11 <!--事务配置文件(注解)-->
12 
13     <!--配置c3p0连接池-->
14     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
15         <!--注入属性-->
16         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
17         <property name="jdbcUrl" value="jdbc:mysql://192.168.184.130:3306/gxrdb"></property>
18         <property name="user" value="root"></property>
19         <property name="password" value="root"></property>
20     </bean>
21 
22     
23     <!-- 第一步、配置事务管理器 -->
24     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
25         <property name="dataSource" ref="dataSource"/>
26     </bean>
27     <!-- 第二步、开启事务的注解 -->
28     <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
29     <!-- 第三步、在事务类上添加注解
30     @Transactional
31     public class UserSerivce {  -->
32 
33     <!--创建service对象,注入dao对象-->
34     <bean id="userSerivce" class="helloworld.txZhuJie.UserSerivce">
35         <property name="userDao" ref="userDao"></property>
36     </bean>
37 
38     <!--创建DAO对象,注入JdbcTemplate对象-->
39     <bean id="userDao" class="helloworld.txZhuJie.UserDao">
40         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
41     </bean>
42 
43     <!--创建JdbcTemplate对象,注入连接池dataSource-->
44     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
45         <property name="dataSource" ref="dataSource"></property>
46     </bean>
47 
48     <!--引入其它配置文件-->
49     <!--<import resource="classpath:helloworld/zhuru/beans.xml"/>-->
50 
51 </beans>

 

posted @ 2018-01-09 18:15  星瑞  阅读(262)  评论(0编辑  收藏  举报