1. 程式人生 > >Spring之事務操作(配置文件)

Spring之事務操作(配置文件)

5.x ati ole 操作 print use point run div

UserDao.java

 1 package helloworld.tx;
 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 add() { 15 // 調用jdbcTemplate對象中的方法實現操作 16 String sql = "insert into salary value(?,?)"; 17 // 表結構:name(varchar 20),salary(int 20) 18 int rows = jdbcTemplate.update(sql, "Tom", 10000); 19 System.out.println("插入行數:" + rows); 20 rows = jdbcTemplate.update(sql, "Jerry", 10000);
21 System.out.println("插入行數:" + rows); 22 } 23 24 // 減少 25 public void reduce(String name,int num) { 26 // 調用jdbcTemplate對象中的方法實現操作 27 String sql = "update salary set salary = (salary - ?) where name= ?"; 28 // 表結構:name(varchar 20),salary(int 20) 29 int
rows = jdbcTemplate.update(sql, num, name); 30 System.out.println("修改行數:" + rows); 31 } 32 33 // 增加 34 public void increase(String name,int num) { 35 // 調用jdbcTemplate對象中的方法實現操作 36 String sql = "update salary set salary = (salary + ?) where name= ?"; 37 // 表結構:name(varchar 20),salary(int 20) 38 int rows = jdbcTemplate.update(sql, num, name); 39 System.out.println("修改行數:" + rows); 40 } 41 42 43 }

UserSerivce.java

 1 package helloworld.tx;
 2 
 3 public class UserSerivce {
 4     private UserDao userDao;
 5 
 6     public void setUserDao(UserDao userDao) {
 7         this.userDao = userDao;
 8     }
 9 
10     public void add(){
11         userDao.add();
12     }
13 
14 //    工資調整
15     public void updateAccount(){
16 
17         userDao.reduce("Tom",1000);
18 
19 //        制造異常
20 //        int n = 100/0;
21 
22         userDao.increase("Jerry",1000);
23 
24     }
25 
26 }

TestTX.java
 1 package helloworld.tx;
 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 * */
12 public class TestTX {
13     @Before
14     public void beforeRun() {
15         System.out.println("beforeRun");
16     }
17 
18 //    插入數據
19     @Ignore
20     @Test
21     public void add() {
22         ApplicationContext context =
23                 new ClassPathXmlApplicationContext("beans_tx.xml");
24         UserSerivce userSerivce = (UserSerivce) context.getBean("userSerivce");
25         userSerivce.add();
26     }
27 
28     @Test
29     public void update() {
30         ApplicationContext context =
31                 new ClassPathXmlApplicationContext("beans_tx.xml");
32         UserSerivce userSerivce = (UserSerivce) context.getBean("userSerivce");
33         userSerivce.updateAccount();
34     }
35 
36 }

beans_tx.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:advice id="txAdvice" transaction-manager="transactionManager">
29         <!--做事務操作-->
30         <tx:attributes>
31             <!--事務操作的方法的匹配規則-->
32             <!--name:方法名;propagation:隔離級別-->
33             <tx:method name="update*" propagation="REQUIRED"/>
34             <tx:method name="add"/>
35         </tx:attributes>
36     </tx:advice>
37     <!-- 第三步、配置切面-->
38     <aop:config>
39         <!--切入點-->
40         <!-- *表示任意方法;..表示任意參數-->
41         <aop:pointcut id="pointcut1" expression="execution(* helloworld.tx.UserSerivce.*(..))"></aop:pointcut>
42         <!--切面-->
43         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"></aop:advisor>
44     </aop:config>
45 
46 
47     <!--創建service對象,註入dao對象-->
48     <bean id="userSerivce" class="helloworld.tx.UserSerivce">
49         <property name="userDao" ref="userDao"></property>
50     </bean>
51 
52     <!--創建DAO對象,註入JdbcTemplate對象-->
53     <bean id="userDao" class="helloworld.tx.UserDao">
54         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
55     </bean>
56 
57     <!--創建JdbcTemplate對象,註入連接池dataSource-->
58     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
59         <property name="dataSource" ref="dataSource"></property>
60     </bean>
61 
62 </beans>

Spring之事務操作(配置文件)