Spring(五)------spring的事务管理

spring事务管理使得我们不用再去处理获得连接,关闭连接,事务提交和回滚等操作,不用大量的try-catch-finally语句

编程式事务管理(不用)

声明式事务管理

  •  基于xml配置文件实现事务管理

 (1)进行事务操作引入spring的最全约束

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>

(2)既然是事务操作,当然也要配置连接池,配置c3p0连接池

	<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///employees"></property>
		<property name="user" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>

(3)配置事务管理器

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>

 (4)配置事务增强规则,指定将增强哪个方法,指定事务管理器,事务隔离级别等

	<tx:advice id="txadvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>

(5)配置切面,将增强用在哪个方法

	<!-- 配置切面 -->
	<aop:config>
		<aop:pointcut expression="execution(* xidian.lili.anno.OrderService.*(..))"
			id="pointcut1" />
		<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1" />
	</aop:config>

(6)dao对象和service对象以及jdbcTemplate对象配置

综上配置文件如下:

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///employees"></property>
		<property name="user" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 配置事务操作 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

	<!-- 配置dao类 -->
	<bean id="orderDao" class="xidian.lili.anno.OrderDao">
		<!-- 向dao类中注入jdbcTemplate属性 -->
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>

	<!-- 配置service类 -->
	<bean id="orderService" class="xidian.lili.anno.OrderService">
		<!-- 向service类中注入dao属性 -->
		<property name="orderDao" ref="orderDao"></property>
	</bean>
	<!-- 创建jdbcTemplate对象 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 向service类中注入连接池属性 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>

</beans>

service类:在service层实现业务逻辑

package xidian.lili.anno;

public class OrderService {

	private OrderDao orderDao;
	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public void add(){
		orderDao.addMoney();
		//int i =10/0;
		orderDao.lessMoney();
	}
}

dao类,在dao类中实现业务方法

package xidian.lili.anno;

import org.springframework.jdbc.core.JdbcTemplate;

public class OrderDao {

	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void addMoney() {
		String sql="update salary set money=money+? where name=?";
		jdbcTemplate.update(sql, "1000","小王");
	}
	public void lessMoney() {
		String sql="update salary set money=money-? where name=?";
		jdbcTemplate.update(sql, "1000","小李");
	}
	
	
}
  • 基于注解方式管理事务

比配置文件简单很多

(1)连接池的配置同上

(2)事务管理器配置同上 

(3)在配置文件中开启事务操作,指定事务管理器

	<!-- 配置事务操作 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

(4)dao对象和service对象以及jdbctemplate对象配置同上

(5)在要进行事务操作的类上添加事务注解,我们的业务逻辑在service层实现,所以在service类添加注解

 综上基于注解配置文件

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">
	
	<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///employees"></property>
		<property name="user" value="root"></property>
		<property name="password" value="123456"></property>
	</bean>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 配置事务操作 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

	<!-- 配置dao类 -->
	<bean id="orderDao" class="xidian.lili.anno.OrderDao">
		<!-- 向dao类中注入jdbcTemplate属性 -->
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean>

	<!-- 配置service类 -->
	<bean id="orderService" class="xidian.lili.anno.OrderService">
		<!-- 向service类中注入dao属性 -->
		<property name="orderDao" ref="orderDao"></property>
	</bean>
	<!-- 创建jdbcTemplate对象 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 向service类中注入连接池属性 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>

</beans>

service类

package xidian.lili.anno;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class OrderService {
	private OrderDao orderDao;
	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}
	public void add(){
		orderDao.addMoney();
		//int i =10/0;
		orderDao.lessMoney();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值