Spring 事务管理(基于xml方式和注解方式)

一。创建数据库

二。建立OrderDao OrderService 和测试类

package springcount;

import org.springframework.transaction.annotation.Transactional;
import c3p0.UserDao;
public class OrderService {
	private OrderDao orderDao;
	public OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}
	public void count(){
		//小明账户减少
		orderDao.lessmoney();
		int i=10/0;
		//小李账户增加
		orderDao.moremoney();
	}
}

package springcount;

import org.springframework.jdbc.core.JdbcTemplate;

public class OrderDao {
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public void lessmoney(){
		String sql="update test3 set sarlary=sarlary-? where username=?";
		jdbcTemplate.update(sql,"1000","xiaowang");
	}
	public void moremoney(){
		String sql="update test3 set sarlary=sarlary+? where username=?";
		jdbcTemplate.update(sql,"1000","xiaoli");
	}
}

package springcount;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestCount {
	@Test
	public void testCount(){
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext2.xml");
		OrderService orderService=(OrderService)context.getBean("orderService");
		orderService.count();
	}
}


三。写applicationContext2.xml文件(基于xml方式)

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd  
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd">
	
	<!-- 配置c3po连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://120.27.104.36:3306/test"></property>
		<property name="user" value="hpn"></property>
		<property name="password" value="hpn2017"></property>
	</bean>
	<!--第一步,配置事务管理器  -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<!--注入dataSource  -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!--第二步,配置事务增强  --> 
	<tx:advice id="txadvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="count"/>
		</tx:attributes>
	</tx:advice>
	<!--第三步,配置切面  --> 
	<aop:config>
		<aop:pointcut expression="execution(* springcount.OrderService.*(..))" id="pointcut1"/>
		<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
	</aop:config>
	<bean id="orderDao" class="springcount.OrderDao">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>
	<bean id="orderService" class="springcount.OrderService">
		<!-- 注入dao对象 -->
		<property name="orderDao" ref="orderDao"></property>		
	</bean>
	<!-- 创建jdbcTemplate对象 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
</beans>

基于注解方式:

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd  
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd">
	
	<!-- 配置c3po连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql://120.27.104.36:3306/test"></property>
		<property name="user" value="hpn"></property>
		<property name="password" value="hpn2017"></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"/>
	<!--第二步,配置事务增强  --> 
<!-- 	<tx:advice id="txadvice" transaction-manager="transactionManager"> -->
<!-- 		<tx:attributes> -->
<!-- 			<tx:method name="count"/> -->
<!-- 		</tx:attributes> -->
<!-- 	</tx:advice> -->
	<!--第三步,配置切面  --> 
<!-- 	<aop:config> -->
<!-- 		<aop:pointcut expression="execution(* springcount.OrderService.*(..))" id="pointcut1"/> -->
<!-- 		<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/> -->
<!-- 	</aop:config> -->
	<bean id="orderDao" class="springcount.OrderDao">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>
	<bean id="orderService" class="springcount.OrderService">
		<!-- 注入dao对象 -->
		<property name="orderDao" ref="orderDao"></property>		
	</bean>
	<!-- 创建jdbcTemplate对象 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
</beans>

另外还需要在OrderService类前加注释@

@Transactional
public class OrderService {
	private OrderDao orderDao;
	public OrderDao getOrderDao() {
		return orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}
	public void count(){
		//小明账户减少
		orderDao.lessmoney();
		int i=10/0;
		//小李账户增加
		orderDao.moremoney();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值