spring进行事务管理

27 篇文章 0 订阅

事务是逻辑上的一组操作,把它看成一个逻辑单元,要么一起成功,要么一起失败

1.导入jar包(spring-tx.jar和spring-jdbc.jar)。用mybatis+spring的基础包就可以 

2.在applicationContext.xml中

  • 配置事务管理器
  • 配置事务通知,设置通知的transactionManager,设置通知的方法
  • 配置事务的aop配置,配置切点和通知(通知指向事务通知,切点指向本切点)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">

	<!--1.数据源 2.扫描包 注册一些bean 3.会话工厂SqlSessionFactory的配置 4.mapper的代理对象bean-->
	
	<!-- 加载外部资源文件 -->
	<context:property-placeholder location="db.properties"/>
	
	<!-- 配置数据库连接池 c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.pwd}"></property>
	</bean>
	
	<!-- 扫描注解自动注册bean -->
	<context:component-scan base-package="com.cbb"></context:component-scan>
	
	<!-- 配置会话工厂,让spring容器来管理会话工厂,单例模式。mybatis-spring.jar下的类 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<!-- 注入属性:数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		
		<!-- 注入属性:mybatis配置文件 -->
		<property name="configLocation" value="mybatis.xml"></property>
	</bean>
	
	
	<!-- mapper的代理bean,包扫描加载的方式批量生成mapper的代理bean。bean的名字就是接口的名字,首字母小写 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定mapper接口的包路径 -->
		<property name="basePackage" value="com.cbb.mapper"></property>
	</bean>
	
	<!-- spring事务管理的配置 -->
	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 事务的通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
		<!-- name:配置的是需要进行事务管理的方法* 是通配符
			propagation 传播行为:
			1).REQUIRED 默认值(缺省的)当前的方法必须运行在事务中,如果当前没有事务则会开启一个新的事务。
			2).SUPPORTS:当前方法如果有事务则使用当前事务,如果没有事务则无需进行事务管理,比较适合于查询
		 -->
			<tx:method name="*" propagation="REQUIRED" />
			<tx:method name="select*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>
	
	<!-- 事务的aop配置 -->
	<aop:config>	
		<aop:pointcut expression="execution(* com.cbb.service..*.*(..))" id="txCut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txCut"/>
	</aop:config>
</beans>

注解的事务开发

  • 配置事务管理器
  • 注册事务的注解驱动 

在要使用事务的方法上面写注解@Transactional

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">

	<!--1.数据源 2.扫描包 注册一些bean 3.会话工厂SqlSessionFactory的配置 4.mapper的代理对象bean-->
	
	<!-- 加载外部资源文件 -->
	<context:property-placeholder location="db.properties"/>
	
	<!-- 配置数据库连接池 c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.pwd}"></property>
	</bean>
	
	<!-- 扫描注解自动注册bean -->
	<context:component-scan base-package="com.cbb"></context:component-scan>
	
	<!-- 配置会话工厂,让spring容器来管理会话工厂,单例模式。mybatis-spring.jar下的类 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<!-- 注入属性:数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		
		<!-- 注入属性:mybatis配置文件 -->
		<property name="configLocation" value="mybatis.xml"></property>
	</bean>
	
	
	<!-- mapper的代理bean,包扫描加载的方式批量生成mapper的代理bean。bean的名字就是接口的名字,首字母小写 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定mapper接口的包路径 -->
		<property name="basePackage" value="com.cbb.mapper"></property>
	</bean>
	
	<!-- spring事务管理的配置 -->
	<!-- 事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据源 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
    <!--事务注解驱动-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值