Spring--------事务管理

                                                 核心

  •     什么是事务?为什么要进行事务管理: 逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部失败
  •     Spring事务管理的核心:AOP的配置 

                                                 基础 

1.事务的特性

  • 原子性:事务不可分割
  • 一致性:事务执行前后数据库完整性保存一致
  • 隔离性:一个事务的执行不应该受到其他事务的干扰
  • 持久性:一旦事务结束,数据就持久化到数据库

2.Spring的事务管理配置:

(1)编程式事务【了解即可】

  • 配置平台事务管理器
  • 配置Spring提供的事务管理的模板类
  • 在业务层上依赖注入事务管理的模板
  • 编写事务管理的代码
  • 测试

(2)基于xml方式的声明式事务【必须掌握】

  • 引入aop的开发包
  • 配置事务管理器
  • 配置增强
  • aop配置
  • 测试

(3)基于注解方式的声明式事务【最简单,必须掌握】

  • 引入aop的开发包
  • 配置事务管理器
  • 开启注解
  • 在业务层添加注解
  • 测试

                            附录一:基于编程式事务配置文件  

项目地址:

  • t1.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: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.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">
	
	<!-- Spring管理bean对象======== -->
	<!-- 配置Service -->
	<bean id = "accountService" class = "com.cyn.spring.tx.demo1.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"/>
		<!-- 注入事务管理的模板 -->
		<property name="transactionTemplate" ref = "transactionTemplate"/>
	</bean>
	
	<!-- 配置Dao -->
	<bean id = "accountDao" class = "com.cyn.spring.tx.demo1.AccountDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"/>
	</bean>
	
	<!-- 配置数据库连接 ========-->
	<!-- 1.配置数据库连接池 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	<!-- 2.配置JDBC的模板 -->
	<bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- Spring事务管理配置======== -->
	<!-- 第一种情况:编程式事务配置【不需要重点记忆】 -->
	<!-- 1.配置平台事务管理器 -->
	<bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 对数据库连接池CRUD操作进行事务监控和管理 -->
		<property name="dataSource" ref="dataSource"/>	
	</bean>
	<!-- 2.配置事务管理的模板 -->
	<bean id = "transactionTemplate" class = "org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager" ref="transactionManager"/>
	</bean>

	
</beans>

                      *附录二:基于xml方式的声明式事务配置文件  

项目地址:

  • t2.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: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.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">
	
	<!-- Spring管理bean对象======== -->
	<!-- 配置Service -->
	<bean id = "accountService" class = "com.cyn.spring.tx.demo2.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"/>
	</bean>
	
	<!-- 配置Dao -->
	<bean id = "accountDao" class = "com.cyn.spring.tx.demo2.AccountDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"/>
	</bean>
	
	<!-- 配置数据库连接 ========-->
	<!-- 1.配置数据库连接池 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	<!-- 2.配置JDBC的模板 -->
	<bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- Spring事务管理配置======== -->
	<!-- 第二种情况:基于xml方式的声明式事务管理配置【需要记忆】 -->
	<!-- 1.配置事务的管理器 -->
	<bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 对数据库连接池内的CRUD操作进行事务监控和管理 -->
		<property name="dataSource" ref="dataSource"/>	
	</bean>
	<!-- 2.配置事务的增强(切面类) -->
	<tx:advice id = "txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 对Spring自带的事务管理增强的方法进行配置 -->
			<!-- 这里的*表示目标类的目标方法进行无论CRUD中的哪个操作,统一采用默认的事务处理 -->
			<tx:method name="*" propagation="REQUIRED"/>
			<!-- 事务管理的规则
			<tx:method name="save" propagation="REQUIRED" isolation = "DEFAULT"/>
			<tx:method name="update" propagation="REQUIRED"/>
			<tx:method name="delete" propagation="REQUIRED"/>
			<tx:method name="find" read-only="true"/> -->
		</tx:attributes>
	</tx:advice>
	<!-- 3.aop的配置(将切面类注册到目标类中进行增强) -->
	<aop:config>
		<!-- 表达式来配置哪些类下的哪些方法需要进行事务管理 -->
		<aop:pointcut expression="execution(* com.cyn.spring.tx.demo2.AccountServiceImpl.*(..))" id = "pointcut1"/>
		<!-- 配置切面:将前面配置好的Spring自带的事务管理切面类注入到需要进行事务控制的目标类的某个方法中  -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
	</aop:config>

	
</beans>

                      *附录三:基于注解方式的声明式事务配置文件  

项目地址:

  • t3.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: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.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">
	
	<!-- Spring管理bean对象======== -->
	<!-- 配置Service -->
	<bean id = "accountService" class = "com.cyn.spring.tx.demo3.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"/>
	</bean>
	
	<!-- 配置Dao -->
	<bean id = "accountDao" class = "com.cyn.spring.tx.demo3.AccountDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"/>
	</bean>
	
	<!-- 配置数据库连接 ========-->
	<!-- 1.配置数据库连接池 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	<bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.username}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	<!-- 2.配置JDBC的模板 -->
	<bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<!-- Spring事务管理配置======== -->
	<!-- 第三种情况:基于注解方式的声明式事务管理配置【需要记忆】 -->
	<!-- 1.配置事务的管理器 -->
	<bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 对数据库连接池内的CRUD操作进行事务监控和管理 -->
		<property name="dataSource" ref="dataSource"/>	
	</bean>
	<!-- 2.开启注解事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	

	
</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值