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>